AppDomains 之间的通信 [英] Communication between AppDomains

查看:29
本文介绍了AppDomains 之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个应用程序(WinForms、.NET 3.5),它将插件"DLL 加载到辅助 AppDomain 中.辅助 AppDomain 需要偶尔与第一个通信(更具体地说,从主 AppDomain 中创建的对象调用或获取数据).

We are building an app (WinForms, .NET 3.5) that loads "Plugin" DLLs into a secondary AppDomain. The secondary AppDomain needs to communicate occasionally with the 1st one (more specifically, call or get data from objects that are created in the main AppDomain).

我已经阅读了大部分关于 AppDomains 和它们之间通信的材料.

I have read most of the material about AppDomains and communication between them.

到目前为止,我见过的唯一简单的解决方案是从 MarshalByRefObject 继承并传递一个 TransparentProxy进入第二个 AppDomain,调用代理上的方法.

So far, the only easy solution i've seen was inheriting from MarshalByRefObject and passing a TransparentProxy into the 2nd AppDomain, calling methods on the Proxy.

此方法有其缺点(例如,在框架类型或已经从另一个类、静态字段/类等继承的类型的情况下,并不总是可以从 MBRO 继承).

This method has its drawbacks (not always possible to inherit from MBRO in case of framework types for example, or types that already inherit from another class, static fields/classes and so on).

由于当前的通信点非常稳定(只有 2-3 个需要通信的场景),我考虑创建一个具有以下属性的简单 Mediator 类:

Since the current communication points are pretty constant (only 2-3 scenarios that require communication), i have considered creating a simple Mediator class with the following properties:

  1. 将在第一个(主)AppDomain 中创建.
  2. 仅作为在主 AppDomain 中创建的真实"对象的消息传递者".
  3. 将从 MBRO 继承,并且对它的代理引用将发送到第二个 AppDomain.

将调用此代理对象上的方法,进而调用第一个 AppDomain 中真实"对象上的方法.

Methods on this proxy object would be called, which in turn will call the methods on the "real" objects in the 1st AppDomain.

我的问题 --

  1. 这看起来是一个合乎逻辑的设计吗?
  2. 更重要的是,WCF 或任何其他通信框架中是否已经存在中介器/消息传递器类?这似乎是一个通用的概念,我想知道是否有类似的东西.

推荐答案

除非您出于某种原因特别想避免使用 WCF,否则我建议您查看一下.具体来说,您可以使用 NetNamedPipeBinding,它使用命名管道在同一台机器上提供通信.您可以在此处找到更多信息:http://msdn.microsoft.com/en-us/library/system.servicemodel.netnamedpipebinding.aspx

Unless you specifically want to avoid WCF for some reason, I would suggest taking a look at it. Specifically, you can use the NetNamedPipeBinding, which provides for communication on the same machine using named pipes. You can find some more information here: http://msdn.microsoft.com/en-us/library/system.servicemodel.netnamedpipebinding.aspx

同样,这里有一个相当简洁的博客条目,展示了它的使用(从 WMP 插件到第三方应用).

As well, here is a reasonably concise blog entry demonstrating it's use (from a WMP plug-in to a third-party app).

根据您对应用程序的描述,您可以在第一个 AppDomain 中建立 WCF 服务,然后从第二个 AppDomain 调用该服务.

Based on your description of the application, you could establish a WCF service in the first AppDomain then call into that service from the second AppDomain.

这篇关于AppDomains 之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆