7

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?

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
Dreamer
  • 3,371
  • 2
  • 34
  • 50

5 Answers5

6

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

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
4

This post is quite complete: Chris Brumme'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.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Salvatore Previti
  • 8,956
  • 31
  • 37
  • Actually the post was quite incomplete. While it briefly states that static fields are initialized in each AppDomain and each AppDomain gets its own copy of static fields, it says nothing about where the data originates. We don't know whether the static data is initialized in the root AppDomain and copied when the other AppDomains are created or if the data in each is instantiated independently at the time the AppDomain is created. Based on the problem with sharing data that I've run into, it appears to be the latter. – Suncat2000 Aug 31 '23 at 03:29
2

In general you will have a copy of data and separate initialization per appdomain.

  1. Yes, there will be a copy of a static class per app domain
  2. No.
  3. Doesn't matter.

If this is a specific question, you might want to share an example of what you are doing. There are marshalling scenarios that will copy data.

driis
  • 161,458
  • 45
  • 265
  • 341
2

You have to deliberately load the static class in each app domain in order to access it, for each app domain it will maintain its own static data.

check this: Static Fields in AppDomain

Community
  • 1
  • 1
Mohamed Abed
  • 5,025
  • 22
  • 31
0

A simple program which prints 0,1,2 and 0,1,2 which shows the appdomain doesn't share static data.

Just modified one of: Static Fields in AppDomain

public static class Class1
{
    private static int Value = 0;
    public static void IncrementAndPrint()
    {
        Console.WriteLine(Value++);
    }
}

public class Foo : MarshalByRefObject
{
    public void Bar()
    {
        Class1.IncrementAndPrint();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var appDomain1 = System.AppDomain.CreateDomain("AppDomain1");
        var appDomain2 = System.AppDomain.CreateDomain("AppDomain2");    

        var class1InAppDomain1 = (Foo)appDomain1.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "ConsoleApplication1.Foo");
        var class1InAppDomain2 = (Foo)appDomain2.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "ConsoleApplication1.Foo");
        class1InAppDomain1.Bar();
        class1InAppDomain1.Bar();
        class1InAppDomain1.Bar();

        class1InAppDomain2.Bar();
        class1InAppDomain2.Bar();
        class1InAppDomain2.Bar();
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Dreamer
  • 3,371
  • 2
  • 34
  • 50