views:

58

answers:

2

I've loaded a specific AppDomain up and I want to load some types dynamically from it (piece of cake right?). The thing is all of the CreateInstance methods of the AppDomain class return things as a remoting object handle. Remoting proxies have limitations that I would like to avoid such as: having to have serializable concrete classes, and over eager garbage collection unless LifeTimeService is used.

My Question is how can I load a type in another app domain without having it wrapped in a remoting proxy? Below is a snippet of my code.

AppDomainSetup ads = new AppDomainSetup();
ads.ApplicationBase = Path.GetDirectoryName(_bllAssemblyPath);
ads.PrivateBinPath =  Path.GetDirectoryName(_bllAssemblyPath);
if (File.Exists(_bllAssemblyPath + ".config"))
    ads.ConfigurationFile = _bllAssemblyPath + ".config";
_workerSpace= AppDomain.CreateDomain("worker", new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence), ads );

_bllQueue = _workerSpace.CreateInstanceFrom(_bllAssemblyPath, queueType) as IThumbCapQueue;
+1  A: 

You will always need some type of proxy to talk between app domains as .NET won't let you directly access the memory of objects in another app domain. Also note that both AppDomains in your example will run within the same Windows Process.

There is a new infrastructure for remoting: RIA Services that might give you the features you want.

David Lynch
+ 1 nice answer.
James
A: 

Instead of making the objects you are interested in remotable, make a remotable "bootstrapper" that serves as a communication channel to the remote AppDomain, and use it to load up what you're interested in. I'm doing this on a project that requires reflecting arbitrary .NET dlls to get type information from them (I do it in a new AppDomain because when I'm done I want to unload the assembly so the file's not locked) - the remoting shim loads the assembly, does reflection, gathers the necessary information and sends it back to the calling AppDomain in the form of serializable objects. See here.

nlawalker
I'm actually looking to just have dynamic concrete instances loaded from disk that are not serialized or remoted.
James
The pattern still holds. Whatever you do, you're going to need to communicate with your secondary AppDomain via a remoting proxy, so you can either make the type you're interested in remotable, or make a remoting shim.
nlawalker
What exactly is a remoting shim?
James
Sorry, I wasn't clear on that: the shim is the "bootstrapper" object I referred to in the original answer. It's a class you create specifically to communicate with the remote AppDomain and use to do stuff inside of that AppDomain. My point was that you're going to need to do remoting if you're working with another AppDomain, so your choices are to make the objects you are interested in remotable so you can communicate directly with them, or create a "shim" you can use to communicate with the other AppDomain.
nlawalker