【问题标题】:Global handling exception in WPF app with Caliburn.Micro使用 Caliburn.Micro 的 WPF 应用程序中的全局处理异常
【发布时间】:2011-02-03 13:46:53
【问题描述】:

您好,我尝试从我的 WPF 应用程序中实现此站点的解决方案以进行全局异常处理。

http://www.codeproject.com/Articles/90866/Unhandled-Exception-Handler-For-WPF-Applications.aspx

我使用 Caliburn Micro 作为 MVVM 框架。我在外部程序集中拥有的服务,它通过 MEF 注入到视图模型类中。

这是我在 WPF 应用程序中的全局异常处理实现。

App.xaml

         DispatcherUnhandledException="Application_DispatcherUnhandledException"
         Startup="Application_Startup"

应用类:

public partial class App : Application
{
    private IMessageBox _msgBox = new MessageBoxes.MessageBoxes();

    public bool DoHandle { get; set; }

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    private void Application_DispatcherUnhandledException(object sender,
                           System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        if (DoHandle)
        {
            _msgBox.ShowException(e.Exception);
             e.Handled = true;
        }
        else
        {
            _msgBox.ShowException(e.Exception);
            e.Handled = false;
        }
    }


    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        var ex = e.ExceptionObject as Exception;
        _msgBox.ShowException(ex);
    }


}

来自外部程序集的服务方法:

    public void ServiceLogOn()
    {
        try
        {

        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

此服务方法在视图模型类中调用,例如在按钮单击事件中:

    [Export(typeof(ILogOnViewModel))]
    public class LogOnViewModel : Screen, ILogOnViewModel
    {
        public void LogOn()
        {
            _service.ServiceLogOn();
        }
    }
  1. 我在 Visual Studio 中运行 WPF 应用程序并在 ServiceLogOn 方法中生成带有“错误凭据”消息的异常。

    我希望看到异常的消息框。

    但 Visual Studio 停止调试应用程序并在服务项目的服务方法中显示异常。

  2. 所以我尝试从 exe 文件运行 WPF 并在 ServiceLogOn 方法中产生相同的异常。

    我收到此错误:

    调用的目标已抛出异常。

视图模型类的任何异常都不会在方法中处理:

  • Application_DispatcherUnhandledException
  • 或 CurrentDomain_UnhandledException。

在 App 类中。

我做错了什么?

根据 Simon Fox 的回答编辑。

我尝试在 Simon Fox 的 MEF 引导程序建议中实施,但我仍然做错了。 我将异常的句柄逻辑移动到引导程序类中的 OnUnhandledException 方法。

这是我的引导程序类代码:

 public class MefBootStrapper : Bootstrapper<IShellViewModel>
    { 
//...
    private IMessageBox _msgBox = new MessageBoxes.MessageBoxes();

    public bool DoHandle { get; set; }

    protected override void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        if (DoHandle)
        {
            _msgBox.ShowException(e.Exception);
            e.Handled = true;
        }
        else
        {
            _msgBox.ShowException(e.Exception);
            e.Handled = false;
        }

    }
//...
    }

我将视图模型中的一些方法绑定到按钮上并抛出新异常。像这样的:

public void LogOn()
{
    throw new ArgumentException("Bad argument");
}

但结果是一样的,我在 Visual Studio 中测试应用程序并得到这个异常。

调用的目标已抛出异常。

【问题讨论】:

  • 异常是否有 InnerException,如果有,它是什么?您可以发布异常中包含的 StackTrace 吗?
  • 嗨,Simon,内部异常为空,如果我在 Visual Studio 中签入,方法 OnUnhandledException 中的代码将被注释执行。
  • Simon 我做了个简单的例子,如果你想你可以试试。这里是:cid-042424ddb4d5fcbf.office.live.com/self.aspx/MSDN/…
  • 您提供的样本对我来说很好用。如果您从 Visual Studio 运行示例而不进行调试 (Ctrl+F5),您应该会得到所需的结果。另一个异常仅在附加调试器时显示,如果您继续执行,您最终将到达OnUnhandledException override
  • 谢谢,Simon,我在使用 Visual Studio 时遇到问题 :)

标签: wpf exception-handling caliburn.micro


【解决方案1】:

Caliburn.Micro 内置了对挂钩未处理异常的支持。 Bootstrapper 类(每个 Caliburn 项目都需要它)为您设置它并提供 virtual OnUnhandledException 方法。

在您的自定义BootStrapper 中,您必须override OnUnhandledException 才能对应用中未处理的异常执行任何自定义操作。请注意,您很可能必须编组操作,例如向 UI 线程显示消息框(Caliburn 通过Execute.OnUIThread 轻松实现这一点)。

您的服务将异常转移到客户端的方式也可能存在问题,但没有任何关于服务如何实现/托管/等的详细信息,我无能为力。你在使用 WCF 做 SOAP 吗?你在用FaultContracts吗?

【讨论】:

  • 嗨 Simon,我没有使用 SOAP 或 REST Web 服务,它只是 HTTP POST/GET 通信,没有 SOAP /JSON。我编辑了我的问题,请看。
  • 这个答案是对的。但是它对我不起作用,因为异常不是从 UI 线程抛出的。我使用的是Tasks,所以我从这个答案中得出建议使用Task.ContinueWith
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
  • 2019-07-26
  • 2012-06-05
相关资源
最近更新 更多