What is the proper way to implement a global Exception catcher-handler in a Katana (OWIN) implementation?
In a self-hosted OWIN/Katana implementation running as an Azure Cloud Service (worker role), I placed this code in a Middleware:
throw new Exception("pooo");
Then I placed this code in the Startup class Configuration method, setting a breakpoint in the event handler:
AppDomain.CurrentDomain.UnhandledException +=
CurrentDomain_UnhandledExceptionEventHandler;
and the event handler in the same class (with a breakpoint set on the first line):
private static void CurrentDomain_UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs e)
{
var exception = (Exception)e.ExceptionObject;
Trace.WriteLine(exception.Message);
Trace.WriteLine(exception.StackTrace);
Trace.WriteLine(exception.InnerException.Message);
}
When the code runs the breakpoint is not hit. The Visual Studio Output window does include this however:
A first chance exception of type 'System.Exception' occurred in redacted.dll
A first chance exception of type 'System.Exception' occurred in mscorlib.dll
I also tried moving the wireup and handler to the Worker Role OnStart method but still the breakpoint is not hit.
I am not using WebAPI at all, but did look at posts on what is done there, but I found nothing clear, so here I am.
Running on .NET Framework 4.5.2, VS 2013.
All ideas appreciated. Thanks.
Try writing a custom middleware and placing it as the first middleware:
Place it as the first middleware:
When we register this middleware as the first middle, any exceptions happening in other middlewares (down the stacktrace) will propagate up and be caught by the
try/catch
block of this middleware.It's not mandatory to always register it as the first middleware, in case you don't need global exception handling for some middlewares, just register these middlewares before this one.
Try this:
And at startup:
@Khanh TO's answer is excellent; Nice terse code, and well explained.
It was not working for me. I added a test exception to one of my .NET MVC controllers like this:
The exception was not caught in the
Invoke
method.This is very easy to debug:
For me, there was code added to a
BaseController
overridingOnException
. This code was handling the exception and not rethrowing. I hope this is a helpful addendum to @Khanh TO's answer.Update
OK, I realize this question is tagged with Web API, and my use case is .NET MVC, but I found this question on a search for Owin middleware. When I debug my solution with the test exception, and watch
$exception
in the Locals view, the exception is gone when I get back to the Owin middleware. Therefore I have not used the code in @Khanh TO's answer. Instead I have added try/catch blocks to my Startup and Global.asax. I have alsoOnException
in my base controller like this:This answer will catch any exceptions on application start up and any exception in a controller. That is enough for my needs.
You can use custom exception filter attributes.