Tôi có một ExceptionFilter toàn cầu trong MVC của tôi \ WebAPI đơn đăng ký như:Làm thế nào để một cách duyên dáng trở lại ngoại lệ từ một WebAPI nhắn Handler
public virtual void RegisterHttpFilters(HttpConfiguration config)
{
config.Filters.Add(new MyExceptionFilter(_exceptionHandler));
}
nơi MyExceptionFilter là:
public class MyExceptionFilter : ExceptionFilterAttribute
{
private readonly IMyExceptionHandler m_exceptionHandler;
public MyExceptionFilter(IMyExceptionHandler exceptionHandler)
{
m_exceptionHandler = exceptionHandler;
}
public override void OnException(HttpActionExecutedContext context)
{
Exception ex = context.Exception;
if (ex != null)
{
object response = null;
HttpStatusCode statusCode = m_exceptionHandler != null
? m_exceptionHandler.HandleException(ex, out response)
: HttpStatusCode.InternalServerError;
context.Response = context.Request.CreateResponse(statusCode, response ?? ex);
}
base.OnException(context);
}
}
Bộ lọc này trả về tất cả trường hợp ngoại lệ là đối tượng json và cho phép một số triển khai IMyExceptionHandler tùy chỉnh đối tượng được trả về.
Tất cả điều này hoạt động tốt. Cho đến khi tôi có ngoại lệ trong một số trình xử lý tin nhắn của mình:
public class FooMessageHandler : DelegatingHandler
{
private readonly Func<IBar> _barFactory;
public FooMessageHandler(Func<IBar> barFactory)
{
_barFactory = varFactory;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Properties["MY_BAR"] = _barFactory();
return base.SendAsync(request, cancellationToken);
}
}
Như bạn có thể thấy trình xử lý này tạo một số thành phần và đưa nó vào thông báo yêu cầu http hiện tại. Khi một ngoại lệ xảy ra trong FuncOfIBar sau đó tôi nhận được màn hình màu vàng của cái chết. ExceptionFilter của tôi không được gọi.
tôi cố gắng đặc biệt bắt ngoại lệ trong xử lý tin nhắn và trở HttpResponseException nhưng nó không thay đổi bất cứ điều gì - vẫn nhận được YSOD:
public class XApplicationInitializerMessageHandler : DelegatingHandler
{
private readonly Func<IXCoreApplication> _appFactory;
private readonly IXExceptionHandler m_exceptionHandler;
public XApplicationInitializerMessageHandler(Func<IXCoreApplication> appFactory, IXExceptionHandler exceptionHandler)
{
ArgumentValidator.EnsureArgumentNotNull(appFactory, "appFactory");
_appFactory = appFactory;
m_exceptionHandler = exceptionHandler;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
request.SetApplication(_appFactory());
}
catch (Exception ex)
{
object resultObject = null;
HttpStatusCode statusCode = m_exceptionHandler != null
? m_exceptionHandler.HandleException(ex, out resultObject)
: HttpStatusCode.InternalServerError;
HttpResponseMessage responseMessage = request.CreateResponse(statusCode, resultObject ?? ex);
throw new HttpResponseException(responseMessage);
}
return base.SendAsync(request, cancellationToken);
}
}
}
Tôi muốn hành vi của ứng dụng của tôi là tương tự bất kể nơi một ngoại lệ xảy ra: trong một ApiController hoặc một trình xử lý tin nhắn.
Cách thực hiện việc này?
Tôi biết về Application_Error nhưng tôi muốn giữ tùy chỉnh HttpApplication không thể chạm tới.
http://stackoverflow.com/questions/13013973/asp-net-web-api-mặc định-lỗi-tin nhắn – Timeless