AppDomain TransparentProxy Objects of Different Type

2/7/2008

I have a very perplexing situation related to AppDomains. I am trying to create a new AppDomain (so I can load arbitrary assemblies and unload them again) but I am unable to cast returned objects to Types that I am able to use.

Specifically I have an object of Type EntityField[] being returned. If I set it to an object I can see that the Type is correct by calling GetType() in the watch window however whenever I cast it from an object to a strongly typed array I get a null value. Here is a little debugger screen shot.

I believe what is happening is that my assembly is being loaded into both AppDomains. So, while the objects are able to correctly identify themselves as the correct types it seems that the runtime is interpretting them as different types, despite the fact that they're coming from the exact same Assembly (even the same file path).

What is the solution to this problem?? Should I not be loading the assembly into both domains? It seems to do this automatically when I try to create the proxy instance. One interesting thing to note is that I had to add an event handler for AppDomain.Current.AssemblyResolve, which I then loop through the local assemblies and return the one who's fullname matches the args.FullName. Is there a more correct way to resolve assemblies? Why is it not resolved automatically, without any help, to begin with?

If anybody knows I'd greatly appreciate some help figuring this out.

As a side note, I'd like to offically vote for the ability to unload assemblies from the current AppDomain. That would be a nice feature.

Comments

Justin Chase - 2/7/2008
I finally figured this out by the way. It turns out that the trick is to define your Proxy object in a seperate assembly from the caller of the proxy and to only return simple types like string or byte[]. Suzanne Cook's blog post was very helpful: <a href="http://blogs.msdn.com/suzcook/archive/2003/06/12/57169.aspx">Executing Code in Another AppDomain</a>
Jason Bock - 2/8/2008
Yes, you want to really isolate what you return over the AppDomain boundary - think of it as a service boundary. It's very tricky to not "leak" types over in assemblies that you currently have loaded in the current domain. BTW that request to unload assmeblies has been around for-EV-er and I doubt it'll ever be done. That said, in 3.5 there's the System.AddIn stuff...have you looked at that? I realize your code base may not be 3.5 yet but it makes AppDomain management much easier.

Add A Comment

Your Name:
Your Email:
Text:
Submit

About Me

Me

My name is Justin Chase and I am a software professional in Minneapolis, Minnesota. I work for a software consulting company called Magenic Technologies and I am the lead developer on an open source project called NBusiness.
More ...

Topics