我想在中间件组件中连接异常处理,如下所示:
public override async Task Invoke(IOwinContext context)
{
try
{
await Next.Invoke(context);
}
catch (Exception ex)
{
// Log error and return 500 response
}
}
Run Code Online (Sandbox Code Playgroud)
但是,HttpErrorResponse在我可以访问之前,我想要捕获的一些异常被Web API管道捕获并转换为s.在这个过程中,我丢失了很多关于错误的细节,所以在调试时我无法得到有用的堆栈跟踪(调试器在抛出异常时甚至不会停止 - 我必须手动单步执行代码并查看它失败的地方......).
我尝试使用以下实现添加自定义异常处理程序:
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
var owinContext = context.Request.GetOwinContext();
owinContext.Set(Constants.ContextKeys.Exception, context.Exception);
return Task.FromResult(0);
}
Run Code Online (Sandbox Code Playgroud)
通过注册config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());在我的启动配置,但在执行后看着它Next.Invoke(context)通过
context.Get<Exception>(Constants.ContextKeys.Exception);
Run Code Online (Sandbox Code Playgroud)
仍然没有给我所有我想要的细节,以及没有在调试器的故障点停止.
有没有办法可以完全关闭所有内置错误处理,以便我自己的中间件可以处理它?
澄清,因为很多人似乎误解了我的追求: