Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2005

Tip: Looking for answers? Try searching our database.

Cast failure using AppDomain.CreateInstanceAndUnwrap

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian Turner - 07 Mar 2005 17:32 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.