如何处理跨应用域数据交换
我们正在构建一个 .NET 应用程序,在其中加载外部代码程序集(“插件”)。 到目前为止,我们将这些程序集加载到单个(主)应用程序域中。
我们希望能够在加载程序集后将其卸载。
为此,我们正在设计一个系统,该系统将创建一个单独的辅助 AppDomain 来托管插件程序集,以便随意卸载。
这种方法存在的问题是:
- 插件 DLL 需要与主 AppDomain 中的类(例如记录器)进行交互。
- 发送到插件 dll 的数据不一定标记为可序列化或派生自 MarshalByRefObj。
在这种情况下,是否有对应用程序进行分区的常见做法?我们可以寻求的最佳解决方案是什么?
另一个有趣的问题——为什么 MarshalByRef 不通过属性并强制我们从对象派生?
We are building a .NET application where we load external code assemblies ("plugins").
Up to this point, we were loading these assemblies into a single (main) application domain.
We would like to be able to unload an assembly after it has been loaded.
To this end, we are designing a system that will create a separate secondary AppDomain to host the plugin assemblies, to be unloaded at will.
The problems we have with this approach:
- The plugin DLL will need to interact with classes in the main AppDomain (logger, for example).
- The data that is sent to the plugin dll is not necessarily marked as Serializable or derived from MarshalByRefObj.
Is there any common practice of partitioning the application in such cases? What is the best solution we could go for ?
Another interesting question -- why doesn't MarshalByRef goes by an attribute and forces us to derive from an object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
跨 AppDomain 进行通信的最简单方法是使用 AppDomain.CreateInstanceAndUnwrap 。这将允许您实例化另一个 AppDomain 中的对象并返回该对象的代理。从代理中,您可以有效地跨应用程序域进行方法调用。
如果您当前的类没有扩展
MarshalByRef
,那么您应该创建一个桥接类来充当中间人。The simplest way to communicate across AppDomains is to use AppDomain.CreateInstanceAndUnwrap . This will allow you to instantiate an object in another AppDomain and return a proxy to that object. From the proxy you can effectively make method calls across the app domain.
If your current classes do not extend
MarshalByRef
then you should create a bridge class that does and can act as a go-between.