传递一个对象以接收从进程中创建的子AppDomain回调到默认AppDomain

时间:2016-03-03 12:59:28

标签: c# ipc appdomain

Sitation:

  1. 我正在从我的流程中创建一个子应用程序域来加载程序集。
  2. 我可以调用此AppDomain。
  3. 我想将一个对象从我的默认进程AppDomain传递到这个新创建的AppDomain,以接收从新AppDomain中加载的程序集到我的默认AppDomain的回调。
  4. 我找到的一种方法是使用AppDomain.DoCallback方法,但是不确定如何在我的子AppDomain中获取我的主机AppDomain?

    任何团体都有任何想法实现它吗?

2 个答案:

答案 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();