选址:
我发现的一种方法是使用AppDomain.DoCallback方法,但是不确定如何在我的子AppDomain中获得宿主AppDomain?
有谁想办法做到这一点?
发布于 2016-03-03 13:50:14
总的思想是将从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发布于 2016-03-03 14:03:38
只需在主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();https://stackoverflow.com/questions/35772869
复制相似问题