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

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

问题描述

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



在自主托管的OWIN / Katana实现中运行一个Azure云服务(工作角色),我把这个代码放在一个中间件中:

  throw new Exception(pooo); 

然后我将此代码放在启动类配置方法中,在事件处理程序中设置断点: / p>

  AppDomain.CurrentDomain.UnhandledException + = 
CurrentDomain_UnhandledExceptionEventHandler;

和同一个类中的事件处理程序(在第一行设置了断点): p>

  private static void CurrentDomain_UnhandledExceptionEventHandler(object sender,UnhandledExceptionEventArgs e)
{
var exception =(Exception) ExceptionObject;
Trace.WriteLine(exception.Message);
Trace.WriteLine(exception.StackTrace);
Trace.WriteLine(exception.InnerException.Message);
}

当代码运行时,断点不受命中。 Visual Studio输出窗口包括但是:

 类型System.Exception的第一次机会异常发生在redacted.dll 
mscorlib.dll中发生类型System.Exception的第一次机会异常

I也尝试将线程和处理程序移动到Worker Role OnStart方法,但是仍然没有打破断点。



我没有使用WebAPI,但是看到了在那里做了什么,但是我没有发现任何事情,所以我在这里。



在.NET Framework 4.5.2,VS 2013上运行。



所有的想法都赞赏。
谢谢。

解决方案

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

  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)
{
//您的处理逻辑
}
}
}
第一个中间件:



<$>

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

builder.Use< GlobalExceptionMiddleware>();
//注册其他中间件
}
}

当我们将这个中间件注册为第一个中间件,在其他中间件(堆栈跟踪)下发生的任何异常将传播,并被这个中间件的 try / catch 块捕获。 p>

始终将其注册为第一个中间件并不是必须的,以防一些中间件不需要全局异常处理,只需在这个中间件之前注册这些中间件。

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

//注册不需要全局异常处理的中间件。
builder.Use< GlobalExceptionMiddleware>();
//注册其他中间件
}
}


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:

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

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

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