I have a static class which has some static data. What happens to the data if its accessed from different app domain?
Will there a copy of a static class for each domain?
Will the primitive types be copied?
What if the data is serializable?
The memory between AppDomain's is not shared. By default the objects are a deep clone, if they are MarshalByRef then its similar to remoting where the calls are executed across AppDomain, so it appears that its shared state.
MarshalByRefObject
is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. Objects that do not inherit fromMarshalByRefObject
are implicitly marshal by value. When a remote application references a marshal by value object, a copy of the object is passed across application domain boundaries.
I don't believe you can actually invoke static members using the AppDomain methods, your best bet would be to wrap the static calls in an instance class and use DoCallback
to execute that code in the other domain and collect the state in a MarshalByRef
object.
See the example on MSDN
This post is quite complete: MSDN Blogs > cbrumme's WebLog > AppDomains ("application domains")
It states:
Whether types are domain-neutral or not, each AppDomain must get its own copy of static fields. And a class constructor must run in each of those AppDomains, to ensure that these static fields are properly initialized.
And I agree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With