Static Variable Instances and AppDomains, what is happening?
Asked Answered
S

2

23

I have

public static class A
{
   public static string ConnString;
}

[Serializable]
public class Test{
   // Accesing A's field;
   public string ConnString{get{return A.ConnString;}set{A.ConnString=value;}}
}

void Main()
{
   A.ConnString = "InitialString"; // I set A.ConnString in the current domain

   var newDomain = AppDomain.CreateDomain("DomNew");
   Test TObj = newDomain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, typeof(Test).FullName) as Test ;

   TObj.ConnString = "NewDomainString"; // It is supposed to set A.ConnString in the newDomain aka a different instance of A.ConnString

   // Here it is supposed to print two different values
   Console.WriteLine(A.ConnString);  // "InitialString"
   Console.WriteLine(TObj.ConnString); // "NewDomainString"
}

But NO! The two WriteLines, print out the same value "NewDomainString"! WHY???

this code

TObj.ConnString = "NewDomainString"

is supposed to change the string in the newly created domain, but it seems they both refer to the same instance!

Why, what is happening here?

Stamp answered 21/3, 2012 at 14:14 Comment(4)
Possible duplicate of https://mcmap.net/q/444826/-static-fields-in-appdomainTenor
Please don't prefix your titles with "C#" and such. That's what tags are for.Rectify
You are right John! Newbie here :) I'm learning! Thanks!Stamp
Daryal you are right. My search was limited to "static variables" and I missed the "static fields" part :)Stamp
M
24

There are only two ways for a class to be accessible from another AppDomain- one is is the class is [Serializable], as your Test class is, the other is if the class inherits from MarshalByRefObject. Because your class is Serializable, a copy of it is created for every cross-AppDomain call. So the Test that the main appdomain gets when you call...

Test TObj = newDomain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, typeof(Test).FullName) as Test;

is actually not the Test instance that was created in the "DomNew" AppDomain- it is a copy local to the "main" AppDomain, and therefore references the static variables from the "main" AppDomain.

If you want Test to exhibit the behavior that you expect, make it inherit from MarshalByRefObject instead of being Serializable.

Moule answered 21/3, 2012 at 15:35 Comment(3)
Nit: there is actually a 3rd way. marshal by bleed bluebytesoftware.com/blog/…Lefebvre
@Lefebvre yikes, yeah... Makes me wince.Moule
Thanx, that was actually what I was looking for. It happened that I found the answer on my own shortly after I made the question. For some reason I didn't try the MarshalByRef inheritance, though I knew there was such a thing. I think i was mislead by an answer in another forum saying that these two ways (Serializable Attribute, and MarshalByRef inheritance) were identical. Though, I think that if Serializable Attirbute is a way to make a "local" copy of the class, you don't actually access the object from another Domain, right? Is it just to "read" an object's state from another domain?Stamp
I
12

You marked your Test class as Serializable. This is wrong. You should have derived from MarshalByRef. Otherwise TObj will just be a local copy in the current AppDomain.

Inna answered 21/3, 2012 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.