【问题标题】:C#: AppDomain UnhandledException ErrorC#:AppDomain UnhandledException 错误
【发布时间】:2012-07-22 10:45:17
【问题描述】:

我创建了一个 AppDomain 并订阅了事件 UnhandledException

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

当我的代码单步执行 UnhandledException 订阅时,它提示错误 "Type 'MainUI.MainWindowViewModel' in assembly 'MainUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is notmarked as serializable. "

编辑: 所以我将 Serializable 放在我的 MainWindowViewModel 类上,但它仍然通过了。当我运行应用程序时,出现相同的错误 “在程序集 'MainUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 中键入 'MainUI.MainWindowViewModel' 未标记为可序列化。”。请帮忙。谢谢

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

编辑

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

我的问题是,如果我有 2 个 appdomain 孩子,我怎么知道这个异常来自哪个孩子?

【问题讨论】:

  • “但它仍然没有工作” - 你得到 same 异常了吗? 不同的例外?还是屏幕变成绿色并带有粉红色斑点?请定义“不起作用”。是否只是要求基类为[Serializable]?还是...?
  • @xscape:我为您提供了答案。如果您不遵循它,您可能需要尝试应用程序域并进行更多的远程处理。一开始很难,但知道这是一件非常有价值的事情。

标签: c# appdomain serializable unhandled-exception


【解决方案1】:

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

您收到错误的原因是该类型试图跨越 appdomain 边界并且未能这样做,因为它不是可序列化的或可远程处理的对象。这也是你不希望发生的事情。

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

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

【讨论】:

  • 嗨 Leppie,只是澄清一下,如果我做对了 UnhandledException 事件必须订阅我的当前域?请看我的编辑。我怎么知道这个异常来自我的一个子域?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多