Last post Sep 21, 2016 08:51 AM by Yohann Lu
Sep 20, 2016 06:17 AM|olegmkr|LINK
How can I handle exceptions which happen in OWIN middlewares?
From Web API 2.1 version there is global error handler, but it doesn't work for middlewares.
Sep 21, 2016 03:24 AM|Yohann Lu|LINK
You can try to create a middleware and registered as the first middleware, then all exceptions will bubble up to that middleware.
1: A custom middleware to hold on exceptions.
public class GlobalExceptionMiddleware : OwinMiddleware
public GlobalExceptionMiddleware(OwinMiddleware next) : base(next)
public override async Task Invoke(IOwinContext context)
// your handling logic
2: Register as the first middleware
public class Startup
public void Configuration(IAppBuilder builder)
var config = new HttpConfiguration();
//register other middlewares
Sep 21, 2016 06:08 AM|olegmkr|LINK
Hi Yohann Lu,
This is understandable case, but I mean global handler, because exception could be in logging middleware and I won't see this in logs. I will get 500 status code and that's it.
Sep 21, 2016 08:51 AM|Yohann Lu|LINK
Please make a test on the recommendation of the community and seriously to find problems. Then, if you still have this issue, It may be today, there’s no easy way in Web API to log or handle errors globally. You can refer the following link.
Global Error Handling in ASP.NET Web API 2: