Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF - Catch exceptions in code executed by SimpleMVVM messagebus
    primarykey
    data
    text
    <p>I'm building a WPF application using the <a href="http://simplemvvmtoolkit.codeplex.com/" rel="nofollow">SimpleMVVM framework</a> and I'm having trouble catching exceptions. I use the MessageBus of SimpleMVVM to send a message to another viewmodel. This all works fine, but I noticed that exceptions raised in the code executed by the messagebus get suppressed. Here's what I've got so far:</p> <p>My <code>MainWindow</code> contains a button that fires a <code>TempCommand</code> on the MainWindowViewModel. This command in turn calls the <code>Test</code> method (shown below), which sends out a notification message using the MessageBus of SimpleMVVM.</p> <pre><code>private void Temp() { SendMessage("Temp", new NotificationEventArgs()); } </code></pre> <p>My <code>MainWindow</code> also contains a <code>Frame</code> with content. The ViewModel of this content, <code>CustomerViewModel</code>, has registered to receive these notifications in its constructor:</p> <pre><code>public CustomerDetailsViewModel(ICustomerServiceAgent agent) { RegisterToReceiveMessages("Temp", Temp); } </code></pre> <p>Where the <code>Temp</code> method simply throws an exception:</p> <pre><code>private void Temp(object sender, NotificationEventArgs args) { throw new NotImplementedException("Somewhere, something horrible happened"); } </code></pre> <p>When I debug the application, I clearly see the <code>Temp</code> method being called and the exception being raised. But for some reason, that's all. The application is unaffected and my exception trapping code is unaware of the exception.</p> <p>I trap exceptions in two ways. The first is by handling the event on the <code>Dispatcher</code>:</p> <pre><code>&lt;Application x:Class="MyApp" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" DispatcherUnhandledException="App_DispatcherUnhandledException"&gt; </code></pre> <p>Where the code-behind looks like:</p> <pre><code>private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { Log("Exception: " + e.Exception.Message); e.Handled = true; } public static void Log(string message) { File.AppendAllText(@"D:\Temp\log.txt", "[" + DateTime.Now.ToString("F") + "] [" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + "] " + message + Environment.NewLine); } </code></pre> <p>This code catches some exceptions, but not all. I found out that WPF suppresses databinding exceptions by default. Because my ViewModels are bounded through the <code>DataContext</code> property on my view, I thought this was the problem. I found <a href="http://tech.pro/tutorial/940/wpf-snippet-detecting-binding-errors" rel="nofollow">this article</a>, which defines a <code>TraceListener</code> that uses the <code>PresentationTraceSources</code> class. Databinding exceptions now get caught, but... Not the exceptions thrown in the code executed through the MessageBus.</p> <p>I've created a solution demonstrating this behavior, it can be downloaded <a href="http://sdrv.ms/11xXVPe" rel="nofollow">here</a>.</p> <p>And this is where I'm stuck. What am I missing? How do I catch these exceptions?</p> <p>Big thanks in advance.</p> <p>JP</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload