Unable to load assembly in appDomain
Asked Answered
D

1

0

Until recently I was loading my assembly by calling Assembly.LoadFrom and it was ok. But now I need to load it in a temporary appDomain but I keep having a FileLoadException when trying to load the assembly in the temp domain. I have tried to pass appDomainSetup parameters to the CreateDomain method but without success.

Here is my code.

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

My assembly is in a sub directory of my application base directory

Difficile answered 10/6, 2013 at 9:5 Comment(0)
M
0

AppDomain.Load loads the assembly in the currently executing AppDomain and not in the "TempDomain" one. As remarked in the MSDN doc:

This method should be used only to load an assembly into the current application domain. This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap.

Now in your case the call fails because the currently executing AppDomain (most probably your main AppDomain) cannot locate assemblies from the sub directory. When you try to load assemblies in the load context, you should make sure that these assemblies are located in one of the following places:

  1. the base dir of the AppDomain
  2. a sub directory of the base dir that is specified in the AppDomain's private bin paths
  3. the GAC

For more info you can check the following articles:

How the Runtime Locates Assemblies

Best Practices for Assembly Loading

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

Miliaria answered 11/6, 2013 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.