Unhandled Exception Global Handler for OWIN / Katana?

拟墨画扇 提交于 2019-11-27 18:30:58
Khanh TO

Try writing a custom middleware and placing it as the first middleware:

public class GlobalExceptionMiddleware : OwinMiddleware
{
   public GlobalExceptionMiddleware(OwinMiddleware next) : base(next)
   {}

   public override async Task Invoke(IOwinContext context)
   {
      try
      {
          await Next.Invoke(context);
      }
      catch(Exception ex)
      {
          // your handling logic
      }
   }
 }

Place it as the first middleware:

public class Startup
{
    public void Configuration(IAppBuilder builder)
    {
        var config = new HttpConfiguration();

        builder.Use<GlobalExceptionMiddleware>();
        //register other middlewares
    }
}

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.

public class Startup
{
    public void Configuration(IAppBuilder builder)
    {
        var config = new HttpConfiguration();

        //register middlewares that don't need global exception handling. 
        builder.Use<GlobalExceptionMiddleware>();
        //register other middlewares
    }
}

Try this:

public class CustomExceptionHandler : IExceptionHandler
{    
    public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
    {
      // Perform some form of logging

      context.Result = new ResponseMessageResult(new HttpResponseMessage
      {
        Content = new StringContent("An unexpected error occurred"),
        StatusCode = HttpStatusCode.InternalServerError
      });

      return Task.FromResult(0);
    }
}

And at startup:

public void Configuration(IAppBuilder app)
{
  var config = new HttpConfiguration();

  config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler());
}

@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:

throw new Exception("Test GlobalExceptionMiddleware");

The exception was not caught in the Invoke method.

This is very easy to debug:

  1. Put a breakpoint on the test exception.
  2. Hit F10 or step over each line until you get to the code that is handling the exception.

For me, there was code added to a BaseController overriding OnException. 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 also OnException in my base controller like this:

protected override void OnException(ExceptionContext filterContext)
{
    if (filterContext.ExceptionHandled)
    {
        return;
    }
    var e = filterContext.Exception;
    Log.Error("BaseController.OnException caught exception:", e);
    filterContext.ExceptionHandled = true;
}

This answer will catch any exceptions on application start up and any exception in a controller. That is enough for my needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!