.NET AppDomain 混乱
我无法找到有关使用 AppDomains 时发生的情况的非常清晰的描述,因此希望有人能够启发我。我有一个简单的测试程序(基本上抄袭了 MSDN 示例):
using System;
using System.Reflection;
class Program
{
public static void Main(string[] args)
{
A localA = new A() { Name = "local" };
localA.PrintAppDomain();
AppDomain domain = AppDomain.CreateDomain("NewDomain");
A remoteA = (A)domain.CreateInstanceAndUnwrap(
Assembly.GetExecutingAssembly().FullName, "A");
remoteA.Name = "remote";
remoteA.PrintAppDomain();
remoteA.PrintA(localA);
remoteA.PrintAppDomain();
}
}
[Serializable]
public class A : MarshalByRefObject
{
public string Name { get; set; }
public void PrintAppDomain()
{
Console.WriteLine("In AppDomain {1}",
this.Name, AppDomain.CurrentDomain.FriendlyName);
}
public void PrintA(A a)
{
Console.WriteLine(a.ToString());
}
public override string ToString()
{
return String.Format("A : {0}", this.Name);
}
}
运行时,打印出
在 AppDomain test.exe 中
在 AppDomain 中 NewDomain
答:本地
在 AppDomain 中 NewDomain
那么...当我执行 remote.PrintA(localA)
时,这是否涉及编组?查看 Reflector 中的 IL 表明并非如此,但我认为一个 AppDomain 中的数据无法访问另一个 AppDomain 中的数据。
如果我从 A
的声明中删除 : MarshalByRefObject
,程序将打印
在 AppDomain test.exe 中
在AppDomain test.exe中
答:本地
在AppDomain test.exe中
这种情况下发生了什么?是否正在创建新的 AppDomain?
I haven't been able to find a very clear description of what's going on when using AppDomains, so hopefully someone will be able to enlighten me. I have a simple test program (basically ripped off the MSDN example):
using System;
using System.Reflection;
class Program
{
public static void Main(string[] args)
{
A localA = new A() { Name = "local" };
localA.PrintAppDomain();
AppDomain domain = AppDomain.CreateDomain("NewDomain");
A remoteA = (A)domain.CreateInstanceAndUnwrap(
Assembly.GetExecutingAssembly().FullName, "A");
remoteA.Name = "remote";
remoteA.PrintAppDomain();
remoteA.PrintA(localA);
remoteA.PrintAppDomain();
}
}
[Serializable]
public class A : MarshalByRefObject
{
public string Name { get; set; }
public void PrintAppDomain()
{
Console.WriteLine("In AppDomain {1}",
this.Name, AppDomain.CurrentDomain.FriendlyName);
}
public void PrintA(A a)
{
Console.WriteLine(a.ToString());
}
public override string ToString()
{
return String.Format("A : {0}", this.Name);
}
}
When run, this prints out
In AppDomain test.exe
In AppDomain NewDomain
A : local
In AppDomain NewDomain
So... when I do remote.PrintA(localA)
, does this involve marshalling? Looking at the IL in Reflector suggests not, but I thought that data in one AppDomain couldn't access data from another AppDomain.
If I remove the : MarshalByRefObject
from the declaration of A
, the program prints
In AppDomain test.exe
In AppDomain test.exe
A : local
In AppDomain test.exe
What is happening in this case? Is a new AppDomain being created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看到的行为是很正常的。
如果您删除
MarshalByRefObject
,因为您具有Serializable
属性,远程处理将为您序列化该类,并将状态编组到主 AppDomain。因此,当该方法运行时,in 会在当前 AppDomain 中运行,因为它位于主 AppDomain 中(已被序列化并编组到当前 AppDomain)。如果保留
MarshalByRefObject
,远程处理将在远程对象上进行调用。如果同时删除两者,则会引发异常,因为远程处理对象需要有一个。
The behaviour you see is quite normal.
If you remove the
MarshalByRefObject
, since you haveSerializable
attribute, remoting will serialize the class for you and marshal the state to the main AppDomain. So when the method runs, in runs in the current AppDomain since it lives in the main AppDOmain (has been serialized and marshalled to current AppDomain).If you keep
MarshalByRefObject
, remoting will make the call on the remote object.If you remove both, it will throw up an exception since remoting objects need to have one.