Dynamically resolve assemblies at runtime

In some situation, it can be useful to provide the runtime additional ways to resolve assemblies. There are two approach for this.

One way is to add .config file settings to specify additional paths for fusion to look at assemblies. This feature is called probing and you can read more about it here:

https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/specify-assembly-location

The other approach is to register an event handler on your app domain and provide the paths programmatically at runtime.

  1. Add a handler to this event
  2. AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver.CurrentDomain_AssemblyResolve;
  3. Inside this handler you can use Assembly.LoadFrom(..) and return the assembly. Note that if you cannot resolve the assembly, simply return null will fall back to fusion load process.

Important to note that if you are loading assemblies from file, I strongly recommend using LoadFrom method instead of Load, as Load from will not be aware of the version you are loading and could lead to performance and stability issues at runtime.

Submit a Comment

Your email address will not be published. Required fields are marked *

Share This