Hi,
I've got a problem with a bit of code I'm using to instantiate objects
in a remote AppDomain.
I've created a class derived from MarshalByRefObject which I
instantiate into a new AppDomain using CreateInstanceAndUnwrap. The
object is instantiated correctly and gives me back a
__TransaparentProxy. However, when I try to cast that to the actual
class, it's throwing an InvalidCastException.
The strange thing is that if I cast it in the Watch Window, it works a
treat.
So, say the class I'm instantiating is MyMbro, when I use
object obj = remotedomain.CreateInstanceAndUnwrap(assemblyName,
typeName);
MyMbro obj1 = (MyMbro) obj;
I get the exception on the second line, but if I just put
(MyMbro) obj
in a watch, it casts correctly and I can see the object.
I've tried everything I can think of to sort this out. I've checked
all the fusion logs and as far as I can tell, both domains are loading
the same assembly (the codebase and display names are consistent)
Does anyone have any pointers for what I should check next?
As a bit of background, the primary appdomain is a BizTalk Adapter. It
is possible that the assembly within the adapter appdomain is loaded
using LoadFrom... Not sure whether that is contributing to the
problem.
Cheers
Ian
"Rick Byers [MSFT]" - 21 Mar 2005 19:15 GMT
Hi Ian,
Have you checked to see if the assembly in question is loaded twice into
your primary AppDomain (where you are doing the cast)? If the assembly was
initially loaded with LoadFrom, then when remoting tries to use the type,
it will attempt to load it in the Load context. If this second Load ends
up finding a different copy then there will be two distinct instances of
the same type, and that will cause exactly this problem (note that this
behavior is by design in order to ensure strong isolation and deterministic
behavior when using LoadFrom). See Suzanne Cook's blog entry on the topic
for more details:
http://blogs.msdn.com/suzcook/archive/2004/06/02/147195.aspx
I hope this helps,
Rick
--------
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> From: iturner100@gmail.com (Ian Turner)
> Subject: Cast failure using AppDomain.CreateInstanceAndUnwrap
[quoted text clipped - 39 lines]
> Cheers
> Ian