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)`
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:
I suggest the 2 option which will allow the folder structure you want plus the Load context which generally the preffered choice.
User contributions licensed under CC BY-SA 3.0