使用全局异常处理弄乱了 DelegatingHandler
using global exception handeling messes up DelegatingHandler
Ovveride IExceptionHandler 时,当发生意外异常时响应不会到达DelegatingHandler。我该如何解决这个问题?
在 webapi 2 中,我想为请求和响应消息实现审计记录器。我还想添加一个全局异常处理程序。但是,当我用自定义实现替换 IExceptionHandler 时。响应永远不会到达 DelegatingHandler -on exception - 因此响应审计丢失。
在 WebApiConfig
// add custom audittrail logger
config.MessageHandlers.Add(new AuditLogHandler());
// replace global exception handeling
config.Services.Replace(typeof(IExceptionHandler), new WebAPiExceptionHandler());
自定义异常处理程序
public class WebAPiExceptionHandler : ExceptionHandler
{
//A basic DTO to return back to the caller with data about the error
private class ErrorInformation
{
public string Message { get; set; }
public DateTime ErrorDate { get; set; }
}
public override void Handle(ExceptionHandlerContext context)
{
context.Result = new ResponseMessageResult(context.Request.CreateResponse(HttpStatusCode.InternalServerError,
new ErrorInformation { Message = "Iets is misgegaan", ErrorDate = DateTime.UtcNow }));
}
}
自定义审计记录器
public class AuditLogHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Content != null)
{
var task = await request.Content.ReadAsStringAsync();
// .. code for loggign request
}
var result = await base.SendAsync(request, cancellationToken);
// .. code for logging response
// when I do not replace WebAPiExceptionHandler, code is reachred here
// When I Do use WebAPiExceptionHandler, code is not reached here
return result;
}
}
webapi抛出异常的代码
public class Values_v2Controller : ApiController
{
public string Get(int id)
{
throw new Exception("haha");
}
}
不要使用 ExceptionHandler 作为基础 class,实现接口 IExceptionHandler
public class WebAPiExceptionHandler : IExceptionHandler
{
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
var fout = new ErrorInformation
{
Message = "Iets is misgegaan"
, ErrorDate = DateTime.UtcNow
};
var httpResponse = context.Request.CreateResponse(HttpStatusCode.InternalServerError, fout);
context.Result = new ResponseMessageResult(httpResponse);
return Task.FromResult(0);
}
private class ErrorInformation
{
public string Message { get; set; }
public DateTime ErrorDate { get; set; }
}
}
问题是 ExceptionHandler
仅在 ShouldHandle(ExceptionHandlerContext context)
return 为真时才执行 Handle(ExceptionHandlerContext context)
方法。
覆盖 bool ShouldHandle(ExceptionHandlerContext context)
始终 return true 解决了我的问题。
Ovveride IExceptionHandler 时,当发生意外异常时响应不会到达DelegatingHandler。我该如何解决这个问题?
在 webapi 2 中,我想为请求和响应消息实现审计记录器。我还想添加一个全局异常处理程序。但是,当我用自定义实现替换 IExceptionHandler 时。响应永远不会到达 DelegatingHandler -on exception - 因此响应审计丢失。
在 WebApiConfig
// add custom audittrail logger
config.MessageHandlers.Add(new AuditLogHandler());
// replace global exception handeling
config.Services.Replace(typeof(IExceptionHandler), new WebAPiExceptionHandler());
自定义异常处理程序
public class WebAPiExceptionHandler : ExceptionHandler
{
//A basic DTO to return back to the caller with data about the error
private class ErrorInformation
{
public string Message { get; set; }
public DateTime ErrorDate { get; set; }
}
public override void Handle(ExceptionHandlerContext context)
{
context.Result = new ResponseMessageResult(context.Request.CreateResponse(HttpStatusCode.InternalServerError,
new ErrorInformation { Message = "Iets is misgegaan", ErrorDate = DateTime.UtcNow }));
}
}
自定义审计记录器
public class AuditLogHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Content != null)
{
var task = await request.Content.ReadAsStringAsync();
// .. code for loggign request
}
var result = await base.SendAsync(request, cancellationToken);
// .. code for logging response
// when I do not replace WebAPiExceptionHandler, code is reachred here
// When I Do use WebAPiExceptionHandler, code is not reached here
return result;
}
}
webapi抛出异常的代码
public class Values_v2Controller : ApiController
{
public string Get(int id)
{
throw new Exception("haha");
}
}
不要使用 ExceptionHandler 作为基础 class,实现接口 IExceptionHandler
public class WebAPiExceptionHandler : IExceptionHandler
{
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
var fout = new ErrorInformation
{
Message = "Iets is misgegaan"
, ErrorDate = DateTime.UtcNow
};
var httpResponse = context.Request.CreateResponse(HttpStatusCode.InternalServerError, fout);
context.Result = new ResponseMessageResult(httpResponse);
return Task.FromResult(0);
}
private class ErrorInformation
{
public string Message { get; set; }
public DateTime ErrorDate { get; set; }
}
}
问题是 ExceptionHandler
仅在 ShouldHandle(ExceptionHandlerContext context)
return 为真时才执行 Handle(ExceptionHandlerContext context)
方法。
覆盖 bool ShouldHandle(ExceptionHandlerContext context)
始终 return true 解决了我的问题。