tags:

views:

69

answers:

2

does the object creating a sub appdomain get instantiated in that sub appdomain?

I have an object that is in the main AppDomain and it is creating another AppDomain and it requires the calling class to be serializable and is creating an instance of the calling class in the new sub AppDomain.

I'm wondering if that is how it is, or if there is a way that I can create the sub appDomain but still hold on to the original instantiation of the calling object in the main appDomain

+1  A: 

Inherit your object from MarshalByRefObject and you won't need to serialize it to make calls across application domain boundaries.

thank you but really I wanted to know if the object creating the sub appdomain gets instantiated in the sub appdomain and why
Eric
@Eric: Probably yes and because you are invoking a method on that object in the app domain. If you are trying to invoke a method in an application domain and that method is instance-scoped, then there has to be a reference to the instance with which the instance is associated. If you make your object a marshal by reference object, then the reference that is created in the other application domain is essentially a proxy to the original in your main application domain. Without code it's impossible to give you exact answers, though; especially on the "why" part.
+1  A: 

No.

You're doing something in your code that is causing the object to be pulled across the domain boundary.

//the current class is creating a domain.  No types exist in the domain
var domain = AppDomain.CreateDomain("2nd Domain");

// create an instance of SomeType in 2nd Doman and create a proxy here
var assembly = typeof(SomeType).Assembly.FullName;
var type = typeof(SomeType).Name;
var proxy = domain.CreateInstanceAndUnwrap(assembly,type);
// at this point, only one instance exists in 2nd Domain.

//These will cause the current class to be pulled across the domain boundary
proxy.Whoops(this);
proxy.SomeEvent += new EventHandler(AMethodDefinedHere);
proxy.Callback = AnotherMethodDefinedHere;

Only when you hand the proxy a pointer to the current instance (and the proxy uses it) does the instance get pulled across the boundary.

Your proxy should only take and return primitive types (like string or byte or arrays of such) or sealed types you define that are serializable or extend MarshalByRefObject.

Will