如何在应用程序域之间传递事件?
我有两个应用程序域,一个父域创建子域。在子域中,有一个 MarshalByRef
对象,使用 .NET Remoting 进行通信。在父域中运行的对象调用远程对象的包装器作为应用程序功能的一部分:
public class ScanningTask : Task
{
private class Loader : MarshalByRef
{
public void Load(IEnumerable<string> paths)
{
...
}
public event EventHandler<LoadEventArgs> OnLoad;
}
public void RunTask()
{
var domain = AppDomain.CreateDomain("LoadDomain");
var loader = (Loader)domain.CreateInstanceFromAndUnwrap(
typeof(Loader).Assembly.Location,
typeof(Loader).FullName);
loader.Load(...);
AppDomain.Unload(domain);
}
}
为简洁起见,删除了大部分代码。
此 Loader
对象公开 OnLoad
我想在父域中捕获的事件。如果我只是添加一个事件处理程序委托,它会尝试将 ScanningTask 序列化到子域中,并抛出有关它不可序列化的异常。
我真正想要的是跨域交流该事件。有什么聪明的建议吗?
I have two app domains, one parent creating the child domain. In the child domain, there is a MarshalByRef
object, being communicated with using .NET Remoting. An object running in the parent domain invokes the wrapper for the remote object as part of the application's function:
public class ScanningTask : Task
{
private class Loader : MarshalByRef
{
public void Load(IEnumerable<string> paths)
{
...
}
public event EventHandler<LoadEventArgs> OnLoad;
}
public void RunTask()
{
var domain = AppDomain.CreateDomain("LoadDomain");
var loader = (Loader)domain.CreateInstanceFromAndUnwrap(
typeof(Loader).Assembly.Location,
typeof(Loader).FullName);
loader.Load(...);
AppDomain.Unload(domain);
}
}
Most code removed for brevity.
This Loader
object exposes an OnLoad
event I'd like to capture in the parent domain. If I just add an event handler delegate, it tries to serialize the ScanningTask
into the child domain and throws an exception about it not being serializable.
What I really want is for the event to be communicated across the domains. Any clever suggestions as to how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于此解决方案,您可以使您的任务类任务也继承自 MarshalByRefObject。这将解决序列化问题,因为它将传递一个跨 AppDomain 序列化引用,该引用将用于附加到事件。
如果由于现有代码库原因,无法使基类 Task 继承自 MarshalByRefObject,则您的解决方案可以是继承自 Loader 的代理类(因此本身就是 MarshalByRefObject)并将调用转发到实际的未包装实例。
Based on this solution, you could make your Task class task inherit from MarshalByRefObject as well. This would solve the serialization issue as it would pass a cross-AppDomain serialized reference which would be used to attach to the event.
If for existing codebase reasons the base class Task cannot be made to inherit from MarshalByRefObject, your solution could be a proxy class that inherits from Loader (therefore being a MarshalByRefObject itself) and forwards calls to an actual unwrapped instance.