Unhandled Exception Global Handler for OWIN / Katana?

后端 未结 4 894
深忆病人
深忆病人 2020-12-02 16:47

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 A

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 17:27

    Try this:

    public class CustomExceptionHandler : IExceptionHandler
    {    
        public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
          // Perform some form of logging
    
          context.Result = new ResponseMessageResult(new HttpResponseMessage
          {
            Content = new StringContent("An unexpected error occurred"),
            StatusCode = HttpStatusCode.InternalServerError
          });
    
          return Task.FromResult(0);
        }
    }
    

    And at startup:

    public void Configuration(IAppBuilder app)
    {
      var config = new HttpConfiguration();
    
      config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler());
    }
    

提交回复
热议问题