OWIN/Katana 的未处理异常全局处理程序? [英] Unhandled Exception Global Handler for OWIN / Katana?

查看:23
本文介绍了OWIN/Katana 的未处理异常全局处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Katana (OWIN) 实现中实现全局异常捕获处理程序的正确方法是什么?

What is the proper way to implement a global Exception catcher-handler in a Katana (OWIN) implementation?

在作为 Azure 云服务(辅助角色)运行的自托管 OWIN/Katana 实现中,我将此代码放在中间件中:

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");

然后我把这段代码放在Startup类的Configuration方法中,在事件处理程序中设置断点:

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);
}

当代码运行时,断点不会被命中.但是,Visual Studio 输出窗口确实包含此内容:

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

我还尝试将接线和处理程序移至 Worker Role OnStart 方法,但仍未命中断点.

I also tried moving the wireup and handler to the Worker Role OnStart method but still the breakpoint is not hit.

我根本没有使用 WebAPI,但确实查看了有关在那里完成的工作的帖子,但我没有发现任何明确的内容,所以我在这里.

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.

在 .NET Framework 4.5.2、VS 2013 上运行.

Running on .NET Framework 4.5.2, VS 2013.

感谢所有想法.谢谢.

推荐答案

尝试编写自定义中间件并将其作为第一个中间件:

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
      }
   }
 }

将其作为第一个中间件:

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

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

当我们将此中间件注册为第一个中间件时,其他中间件(沿堆栈跟踪)发生的任何异常都将向上传播并被此中间件的 try/catch 块捕获.

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
    }
}

这篇关于OWIN/Katana 的未处理异常全局处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆