[英]Subscribe to DomainUnload event from a main AppDomain
I'm creating an application which works with a new AppDomain that can be created and unloaded.我正在创建一个应用程序,它与一个可以创建和卸载的新 AppDomain 一起使用。 I'd like to handle the DomainUnload event so I need to subscribe to it.我想处理 DomainUnload 事件,所以我需要订阅它。 I can't subscribe to this event from the new AppDomain that can be unloaded as all members from there will not be accessible once the domain is unloaded.我无法从可以卸载的新 AppDomain 订阅此事件,因为卸载域后将无法访问那里的所有成员。
But when I try to subscribe to this event from Main AppDomain I always getting the exception: " System.Runtime.Serialization.SerializationException: Type 'MyNamespace.MainWindow' in assembly 'MyMainAssembly' is not marked as serializable. "但是当我尝试从 Main AppDomain 订阅此事件时,我总是收到异常:“ System.Runtime.Serialization.SerializationException: Type 'MyNamespace.MainWindow' in assembly 'MyMainAssembly' 未标记为可序列化。 ”
Initially I tried to add an attribute [Serializable] to class in the main app domain, but it didn't help.最初,我尝试向主应用程序域中的类添加一个属性 [Serializable],但没有帮助。 Then I created a new class 'RemoteHandlerClass' derived from MarshalByRefObject and put the subscription code in it, but I'm also getting the same error.然后我创建了一个从 MarshalByRefObject 派生的新类“RemoteHandlerClass”并将订阅代码放入其中,但我也遇到了同样的错误。 Using the [Serializable] attribute in this class also didn't help.在这个类中使用 [Serializable] 属性也没有帮助。 To access members from this class I use proxy object in main app domain created by:要访问此类中的成员,我在主应用程序域中使用代理对象,由以下人员创建:
var remoteHandler = (RemoteHandlerClass)workerDomain.CreateInstanceAndUnwrap(
typeof(RemoteHandlerClass).Assembly.FullName, typeof(RemoteHandlerClass).FullName);
How can I subscribe to this event?我如何订阅此活动? Preferably I would like to subscribe to this event from the main app domain.最好我想从主应用程序域订阅此事件。 The code below can reproduce my issue.下面的代码可以重现我的问题。
Thanks!谢谢!
public partial class MainWindow : Window
{
private void Button_Click(object sender, RoutedEventArgs e)
{
var newDomain = AppDomain.CreateDomain("New domain");
newDomain.DomainUnload += UnloadHandler; // System.Runtime.Serialization.SerializationException:
// Type 'MyNamespace.MainWindow' in assembly 'MyMainAssembly'
// is not marked as serializable.
}
private void UnloadHandler(object sender, EventArgs e)
{
}
}
So, when dealing with AppDomains, your handler object needs to be MarshalByRefObject
which its not -- it's just a Window type.因此,在处理 AppDomains 时,您的处理程序对象需要是MarshalByRefObject
而不是——它只是一个 Window 类型。 Wrap what you are doing in a simple class such as将您正在做的事情包装在一个简单的类中,例如
class DomainContainer : MarshalByRefObject
{
public void StartDomain()
{
var newDomain = AppDomain.CreateDomain("New domain");
newDomain.DomainUnload += UnloadHandler;
}
private void UnloadHandler(object sender, EventArgs e)
{
}
}
and call that in your app.并在您的应用程序中调用它。
private void Button_Click(object sender, RoutedEventArgs e)
{
var d = new DomainContainer();
d.StartDomain();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.