AppDomain.CreateInstanceFromAndUnwrap also loads assembly in main AppDomain

154 Views Asked by At

It seems this question has been asked before, but none of the answers around here on StackOverlow actually solve my issue, though some answers say it should.

What I see, is when I call AppDomain.CreateInstanceFromAndUnwrap to load create a type in another AppDomain than the main AppDomain, the assembly that holds that type is also loaded in the main application domain and that is what I do not want.

I have the following code:

AppDomainSetup appDomainSetup = new AppDomainSetup();
appDomainSetup.ApplicationName = @"TestDomain";
appDomainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
AppDomain testDomain = AppDomain.CreateDomain(appDomainSetup.ApplicationName, null, appDomainSetup);

object other = testDomain.CreateInstanceFromAndUnwrap(@"TestLib.dll", @"TestLib.Class1");

When I print the assemblies loaded in the main application domain before I call CreateInstanceFromAndUnwrap, the TestLib.dll is not loaded in the main application domain, while it is loaded in the main application domain right after this call!

Also changing the `CreateInstanceFromAndUnwrap call to:

byte[] assemblyBytes = File.ReadAllBytes(@".\TestLib.dll");
testDomain.Load(assemblyBytes);

will result in the TestLib.dll being loaded in both application domains.

What can I do to prevent the TestLib.dll to be loaded in the main application domain?

0

There are 0 best solutions below