Sitation:
我找到的一种方法是使用AppDomain.DoCallback方法,但是不确定如何在我的子AppDomain中获取我的主机AppDomain?
任何团体都有任何想法实现它吗?
答案 0 :(得分:4)
一般的想法是向新创建的域传递一个派生自MarshalByRefObject
类的类的实例。它将保证此对象将通过引用而不是值进行封送处理。这意味着代理将传递给新域而不是原始对象(此代理将由.NET框架为您生成)。
稍后当您在此代理上调用方法时,此调用将传递回原始域(创建对象的域)。换句话说,一个方法将在原始域中执行。
这是一个显示这个想法的代码:
public class Program
{
private static void Main(string[] args)
{
var listener = new Listener();
var otherDomain = AppDomain.CreateDomain("otherDomain");
var instance = (Loader)otherDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Loader).FullName);
instance.Init(listener);
}
}
[Serializable]
public class Loader
: MarshalByRefObject
{
public void Init(Listener listener)
{
Console.WriteLine($"[{nameof(Init)}] Hello from {AppDomain.CurrentDomain.FriendlyName} domain");
listener.Callback();
}
}
[Serializable]
public class Listener
: MarshalByRefObject
{
public void Callback()
{
Console.WriteLine($"[{nameof(Callback)}] Hello from {AppDomain.CurrentDomain.FriendlyName} domain");
}
}
运行此代码时,您将获得以下结果:
[Init] Hello from otherDomain domain
[Callback] Hello from Sandbox.vshost.exe domain
它显示Init方法在新域中执行,但在原始域中执行回调。现在用: MarshalByRefObject
注释2行并再次运行该程序。此时间Listener
将按值传递到新域,结果将为:
[Init] Hello from Sandbox.vshost.exe domain
[Callback] Hello from Sandbox.vshost.exe domain
答案 1 :(得分:2)
只需在主AppDomain
中创建远程主机对象,并将其传递给新初始化的子域。只要孩子想要将数据发送到主机,请使用此远程主机对象。
// This class provides callbacks to the host app domain.
// As it is derived from MarshalByRefObject, it will be a remote object
// when passed to the children.
// if children are not allowed to reference the host, create an IHost interface
public class DomainHost : MarshalByRefObject
{
// send a message to the host
public void SendMessage(IChild sender, string message)
{
Console.WriteLine($"Message from child {sender.Name}: {message}");
}
// sends any object to the host. The object must be serializable
public void SendObject(IChild sender, object package)
{
Console.WriteLine($"Package from child {sender.Name}: {package}");
}
// there is no timeout for host
public override object InitializeLifetimeService()
{
return null;
}
}
我怀疑您创建的子对象已经实现了一个接口,因此您可以从主域引用它们而不加载它们的实际类型。在初始化时,您可以将它们传递给宿主对象,因此在初始化之后,您可以从子节点进行回调。
public interface IChild
{
void Initialize(DomainHost host);
void DoSomeChildishJob();
string Name { get; }
}
<强> ChildExample.dll:强>
internal class MyChild : MarshalByRefObject, IChild
{
private DomainHost host;
public void Initialize(DomainHost host)
{
// store the remote host here so you will able to use it to send feedbacks
this.host = host;
host.SendMessage(this, "I am being initialized.")
}
public string Name { get { return "Dummy child"; } }
public void DoSomeChildishJob()
{
host.SendMessage(this, "Job started.")
host.SendObject(this, 42);
host.SendMessage(this, "Job finished.")
}
}
<强>用法:强>
var domain = AppDomain.CreateDomain("ChildDomain");
// use the proper assembly and type name.
// child is a remote object here, ChildExample.dll is not loaded into the main domain
IChild child = domain.CreateInstanceAndUnwrap("ChildExample", "ChildNamespace.MyChild") as IChild;
// pass the host to the child
child.Initialize(new DomainHost());
// now child can send feedbacks
child.DoSomeChildishJob();