异常处理无可非议的过滤器? [英] Exception handling beyond Exception Filters?

查看:208
本文介绍了异常处理无可非议的过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Asp.net的WebAPI(RC),我怎么能赶上不由的异常过滤器的Application_Error()在Global.asax中?

Using Asp.net WebApi (RC), how can I catch errors that are not caught by Exception Filters or Application_Error() in global.asax?

使用这两种到位似乎还有一类例外情况仍然不是盖的。例如: ApiControllerActionSelector_AmbiguousMatch 错误(多个操作中发现匹配的要求,即:{0} )。

With both of these in place it seems that there is a class of exceptions still not covered. For example: ApiControllerActionSelector_AmbiguousMatch error (Multiple actions were found that match the request: {0}).

我不是特别关心的上述错误的,这个错误只是指出,有一类错误而不是由我的任何异常过滤器或的Application_Error 方法。

I'm not specifically concerned about the above error, this error just pointed out that there is a class of errors that aren't being caught by either my Exception Filter or Application_Error method.

所以,我怎么能覆盖所有我的基地?

So how can I cover all my bases?

推荐答案

您说得对,有几类例外不受任何的Application_Error或ExceptionFilter被困。在Web API请求管道是分开ASP.NET MVC管道(至少在MVC 4)处理,所以MVC的Application_Error不踢项。另外,如果您的应用程序引发的Htt presponseException 键入例外,他们的不会由捕获 ExceptionFilter 设计(请参阅 ExceptionFilter 章节)。要访问抛出的所有异常的的code,你需要创建一个 DelegatingHandler 沿着这code的线路:

You're right, there are several classes of exception not trapped by either Application_Error or ExceptionFilter. The Web API request pipeline is processed separately from the ASP.NET MVC pipeline (at least through MVC 4) so the MVC Application_Error doesn't kick-in. Also, if your application throws HttpResponseException type exceptions, they will not be caught by an ExceptionFilter by design (see the ExceptionFilter paragraph). To access all exceptions thrown by your code, you'll need to create a DelegatingHandler along the lines of this code:

public class ResponseExceptionTrapper : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        return base
            .SendAsync(request, cancellationToken)
            .ContinueWith(response =>
                 {
                     var result = response.Result;
                     if (!result.IsSuccessStatusCode)
                     {
                          var exceptionResult = string.Format(
                               "Response exception: Path({0}) Status({1}) ",
                               request.RequestUri,
                               result.StatusCode);

                          if (result.Content != null)
                          {
                               var exceptionReadTask =
                                      result.Content.ReadAsStringAsync();

                               exceptionReadTask.Wait();
                               exceptionResult += "Message:\n\r" +
                                                 exceptionReadTask.Result;
                           }

                           // Do something appropriate with exceptionResult
                      }

                      return result;
                 }, cancellationToken);
    }
}

您可以连线了该行的处理程序在全局配置逻辑:

You can wire up the handler with this line in your global config logic:

GlobalConfiguration.Configuration.MessageHandlers.Add(
     new ResponseExceptionTrapper());

这篇关于异常处理无可非议的过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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