未处理的异常处理程序全球对OWIN /武士刀? [英] Unhandled Exception Global Handler for OWIN / Katana?

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

问题描述

什么是实现卡塔纳(OWIN)实施全球异常捕手处理程序的正确方法?

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

在自托管OWIN /武士刀实施运行作为Azure的云服务(工作者的角色),我把这个code在中间件:

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

然后我把这个code在启动类的配置方法,在事件处理程序中设置断点:

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

当code运行断点未命中。在Visual Studio Output窗口但不包括此:

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

我也试过移动wireup和处理程序来辅助角色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框架4.5.2,VS 2013。

Running on .NET Framework 4.5.2, VS 2013.

所有的想法AP preciated。
谢谢你。

All ideas appreciated. Thanks.

推荐答案

尝试编写自定义的中间件,并把它作为第一中间件:

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 /武士刀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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