Global processing exception in a WPF application using Caliburn.Micro

Hi, I am trying to implement a solution from this site in my WPF application for handling global exceptions.

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

I use Caliburn Micro as an MVVM framework. The service that I have in the external assembly, and it is introduced into the model class with MEF.

Here is my implementation for handling global exceptions in a WPF application.

App.xaml

         DispatcherUnhandledException="Application_DispatcherUnhandledException"
         Startup="Application_Startup"

Application Class:

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);
    }


}

Service method from external assembly:

    public void ServiceLogOn()
    {
        try
        {

        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

This service method is a call to the view model class, for example, when a button is clicked:

    [Export(typeof(ILogOnViewModel))]
    public class LogOnViewModel : Screen, ILogOnViewModel
    {
        public void LogOn()
        {
            _service.ServiceLogOn();
        }
    }
  • I run the WPF application in Visual Studio and throw an exception with the message "Bad credentials" in the ServiceLogOn method.

    I expect that I will see a window with an exception.

    Visual Studio .

  • , WPF exe ServiceLogOn.

    :

    .

:

  • Application_DispatcherUnhandledException
  • CurrentDomain_UnhandledException.

App.

?

.

MST- , - - . OnUnhandledException bootstraper.

bootstraper:

 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");
}

sam, Visual Studio .

.

+5
1

Caliburn.Micro . Bootstrapper ( Caliburn) virtual OnUnhandledException.

BootStrapper override OnUnhandledException . , , , , (Caliburn Execute.OnUIThread).

, , - , //etc, . WCF SOAP? FaultContracts?

+13

All Articles