Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to the static data in a class if it is accessed across app domains?

Tags:

.net

appdomain

I have a static class which has some static data. What happens to the data if its accessed from different app domain?

  1. Will there a copy of a static class for each domain?

  2. Will the primitive types be copied?

  3. What if the data is serializable?

like image 923
Dreamer Avatar asked Oct 31 '11 18:10

Dreamer


2 Answers

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 from MarshalByRefObject 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

like image 171
Paul Tyng Avatar answered Sep 23 '22 11:09

Paul Tyng


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.

like image 20
Salvatore Previti Avatar answered Sep 25 '22 11:09

Salvatore Previti