异常过滤器之外的异常处理?
Exception handling beyond Exception Filters?
使用 Asp.net WebApi (RC),我怎样才能捕获 Exception Filters 或 global.asax 中的 Application_Error() 未捕获的错误?
有了这两个条件,似乎还有一类例外情况仍未涵盖。例如:ApiControllerActionSelector_AmbiguousMatch 错误 (Multiple actions were found that match the request: {0}).
我并不特别关心上述错误,这个错误只是指出有一类错误没有被我的异常过滤器或 Application_Error 方法捕获。
那么我怎样才能覆盖我所有的基地呢?
你说得对,有几类异常没有被 Application_Error 或 ExceptionFilter 捕获。 Web API 请求管道与 ASP.NET MVC 管道分开处理(至少通过 MVC 4),因此 MVC Application_Error 不会启动。此外,如果您的应用程序抛出 HttpResponseException 类型的异常,它们将不会被设计的 ExceptionFilter 捕获(请参阅 ExceptionFilter 段落)。要访问您的代码引发的所有异常,您需要按照以下代码行创建一个 DelegatingHandler:
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:\
\
" +
exceptionReadTask.Result;
}
// Do something appropriate with exceptionResult
}
return result;
}, cancellationToken);
}
}GlobalConfiguration.Configuration.MessageHandlers.Add(
new ResponseExceptionTrapper());
您可以在全局配置逻辑中使用此行连接处理程序:
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:\
\
" +
exceptionReadTask.Result;
}
// Do something appropriate with exceptionResult
}
return result;
}, cancellationToken);
}
}GlobalConfiguration.Configuration.MessageHandlers.Add(
new ResponseExceptionTrapper());
我相信只有在调用操作时才会调用异常过滤器(在这种情况下,它周围有一个 try/catch)。歧义匹配错误会在此之前在管道中弹出,之后可能会弹出其他错误(例如格式化程序错误),正如您所提到的。
我不确定您是否可以使用一种解决方案来解决所有方面的问题(因为托管实现可能会有所不同),但您可以尝试覆盖 HttpControllerDispatcher。此类是管道中使用的"根"类之一。具体来说,您可以覆盖 SendAsync 来执行您的 try/catch 并相应地处理。