Failed to load assembly in appDomain

Until recently, I loaded my assembly by calling Assembly.LoadFrom, and everything was fine. But now I need to load it into a temporary appDomain, but when I try to load the assembly in a temporary domain, I get a FileLoadException. I tried passing the AppDomainSetup parameters to the CreateDomain method, but to no avail.

Here is my code.

var tempDomain = AppDomain.CreateDomain("TempDomain"); Assembly sampleAssembly = tempDomain.Load(pathToDll); 

My build is in a subdirectory of my base application directory

0
source share
1 answer

AppDomain.Load loads the assembly in the current executable AppDomain, and not in "TempDomain". As noted in the MSDN document:

This method should only be used to load the assembly into the current application domain. This method is provided as a convenience to compatible subscribers who cannot invoke the static assembly Assembly.Load method. To load assemblies into other application areas, use a method such as CreateInstanceAndUnwrap.

Now, in your case, the call fails because the currently executing AppDomain (most likely your main AppDomain) cannot find the assemblies from the subdirectory. When you try to load assemblies in the context of the load, you must ensure that these assemblies are located in one of the following places:

  • AppDomain base directory
  • auxiliary directory of the base dir, which is specified in the personal application pools of AppDomain
  • Gac

For more information, you can check out the following articles:

How Runtime Finds Assemblies

Assembly loading guidelines

Back to Basics: Using Fusion Log Viewer to Debug Obscure Loader Errors

0
source

Source: https://habr.com/ru/post/947793/


All Articles