2012-07-24 15 views
1

我创建一个AppDomain和预订事件UnhandledExceptionC#:AppDomain中UnhandledException错误

AppDomain sandbox1 = AppDomain.CreateDomain("SandBox1"); 
sandbox1.UnhandledException += 
    new UnhandledExceptionEventHandler(sandbox1_UnhandledException); 

当通过UnhandledException订阅我的代码的步骤,它在装配提示出错“类型“MainUI.MainWindowViewModel“MainUI ,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'未标记为可序列化。“

编辑: 所以我把序列化我MainWindowViewModel类,但仍然它没有经历过。当我运行应用程序相同的错误去“类型'MainUI.MainWindowViewModel'在程序集'MainUI,版本= 1.0.0.0,文化=中性,PublicKeyToken = null'未被标记为可序列化。。请帮忙。谢谢

[SecurityPermission(SecurityAction.Demand, 
        Flags = SecurityPermissionFlag.AllFlags)] 
public class MainWindowViewModel : ViewModelBase 
{......} 

编辑

public MainWindowViewModel() 
{ 
    AppDomain current = AppDomain.CurrentDomain; 
    current.UnhandledException += 
     new UnhandledExceptionEventHandler(current_UnhandledException); 
} 

我的问题在这里,如果我有2个孩子的AppDomain,我怎么知道,这个例外是来自哪个孩子?

+0

“但它仍然没有工作” - 你有*相同*例外吗?一个*不同*异常?或者屏幕变成粉红色斑点的绿色?请定义“没有工作”。它只是要求基类是'[Serializable]'?要么...? – 2012-07-24 06:42:26

+0

请看我的编辑 – xscape 2012-07-24 06:48:18

+0

@xscape:我提供了你的答案。如果你不遵循它,你可能需要尝试一下appdomains和remoting。起初相当困难,但知道的非常有价值。 – leppie 2012-07-24 07:04:03

回答

3

这绝对不是这样做的方式。

您收到错误的原因是该类型正在尝试跨越应用程序域边界,并因此失败,因为它不是可序列化的或远程对象。这也是你不希望发生的事情。

作为一种解决方案,您应该在沙箱应用程序域中订阅该事件。

在该处理程序中,您可以通过您选择的某个远程接口/服务将异常信息传输到“主”应用程序域。

+0

嗨Leppie,只是澄清,如果我得到它的权利UnhandledException事件必须订阅我的currentdomain?请参阅我的编辑。我如何知道这个异常来自我的一个子域? – xscape 2012-07-24 07:51:20

相关问题