CreateInstanceAndUnwrap fails to load assembly

6

I am trying to load an assembly A in a new AppDomain in my console application with same base directory and RelativePath as the default domain.

When I create a type from A using CreateInstanceFrom it succeeds but when I use CreateInstanceAndUnwrap it fails to find assembly file with FileLoadException. Keep in mind that assembly A.MyType calls a method from assembly B.Typeb in it's constructor. Both assemblies files are present at the same path in parent folder of executing assembly (..\Mytypes)

_domain = AppDomain.CreateDomain("MyDomain" + Guid.NewGuid(), null, AppDomain.CurrentDomain.SetupInformation);
var mytype = _domain.CreateInstanceAndUnwrap(pathtoassembly, typename);

Here is the error message:

Could not load file or assembly '..\Mytypes\A.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)`

c#
reflection
appdomain
asked on Stack Overflow Apr 8, 2013 by iamadnan • edited Nov 20, 2017 by Ehsan Sajjad

1 Answer

16

AppDomain.CreateInstanceAndUnwrap will load the assembly in the Load context while the AppDomain.CreateInstanceFromAndUnwrap will load the assembly in the LoadFrom context.

The reason it work with the CreateInstanceFromAndUnwrap method is that the LoadFrom context will try to resolve assemblies in the Mytypes folder. The Load context will not. It will try to resolve only from the GAC, the BaseDirectory and the RelativeSearchPath of the AppDomain.

Some of the options you have are:

  1. Use the LoadFrom context (by using the CreateInstanceFromXXX methods).
  2. Add the Mytypes folder to the AppDomainSetup.PrivateBinPath used to create the AppDomain. This way the Load context will be able to resolve the assebmlies located there.
  3. Subscribe to the AppDomain.AssemblyResolve event and resolve the assemblies yourself by looking for them and loading them from the Mytypes folder.
  4. Deploy all your assemblies in the base directory of your application.

I suggest the 2 option which will allow the folder structure you want plus the Load context which generally the preffered choice.

answered on Stack Overflow Apr 9, 2013 by Panos Rontogiannis

User contributions licensed under CC BY-SA 3.0