[WPF] Handling unhandled exceptions with async/await

Topics: Bootstrappers & IoC, UI Architecture
May 9, 2014 at 1:05 PM
Edited May 12, 2014 at 7:51 AM
Hi,

I'm trying to take care of unhandled exceptions with async/await on a wpf project. The main purpose is to keep track of the exceptions as it seems that caliburn is swallowing those exceptions in async methods (same as this thread and this one).
So far, I found this approach who uses SynchronizationContext but it's based on a Win8 application.

So I tried to "register" my SynchronizationContext in my App.xaml.cs but the SynchronizationContext.current is always null :
 public partial class App : Application
    {
        public App()
            : base()
        {
            AsyncSynchronizationContext.Register();
        }
    }
 public class AsyncSynchronizationContext : SynchronizationContext
    {
        public static AsyncSynchronizationContext Register()
        {
            
            var syncContext = Current;
            if (syncContext == null)
                throw new InvalidOperationException("Ensure a synchronization context exists before calling this method.");

            var customSynchronizationContext = syncContext as AsyncSynchronizationContext;

            if (customSynchronizationContext == null)
            {
                customSynchronizationContext = new AsyncSynchronizationContext(syncContext);
                SetSynchronizationContext(customSynchronizationContext);
            }

            return customSynchronizationContext;
        }
...
}
Should I register somewhere else in the Bootstrapper ?
Maybe there's other way to manage those exceptions with async/await.

Any suggestions?

Thanks,
nico
May 10, 2014 at 4:32 PM
May 12, 2014 at 8:45 AM
Thanks for the link but I ended with the same solution (SynchronizationContext).

I edited my first post to be a bit more specific as the real problem here is with Caliburn.