Martin Blore
Martin Blore

Reputation: 2195

Current AppDomain loading assemblies outside of application base?

I'm creating a new sandbox AppDomain whose ApplicationBase and PrivateBinPath (for example sake) has been set to C:\MyApp. My executing application is running from C:\SomewhereElse.

When I otherDomain.Load(...) an assembly, my executing AppDomain is also loading the assembly. I'm determining this by checking GetAssemblies() before the load, and then also the GetAssemblies() after the load.

Why is this happening? I suspect it has something to do with the meta-data needing to be available in the executing AppDomain and it's passed back over from the new domain via 'Cross Boundary', so the calling domain is loading the assembly too. But! I thought that an assembly couldn't be loaded outside of it's ApplicationBase, unless it was in the GAC, which in this case, it's not.

Can anyone help with my confusion?

Upvotes: 2

Views: 663

Answers (1)

Oblivion2000
Oblivion2000

Reputation: 616

In order to not load the second appdomain's assemblies into the parent domain, you can't use otherdomain.Load(...). You have to create a MarshalByRefObject in the child appDomain, and have that code call AppDomain.Load(...).

Example:

public class AppDomainInitializer : MarshalByRefObject
{
  public void Initialize() { AppDomain.Load(...); }
}

Parent Domain:

{
AppDomain otherDomain = AppDomain.CreateDomain(...);

// Create the object in the other domain
ObjectHandle oh = Activator.CreateInstance(otherDomain, assemblyNme, "AppDomainInitializer", ...);

// Marshall it to this domain
var initializer = (AppDomainInitializer) oh.UnWrap();

// Proxy the call to load up the other domain dll's
intializer.Initialize();    
}

Upvotes: 1

Related Questions