Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

c# - Unhandled Exception Global Handler for OWIN / Katana?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...