在我工作的项目中,使用了服务堆栈以及 ServiceBase 类的 HandleException 方法的下一个 ovveriding:
public abstract class BaseAggregationService<TRequest, TResponse> : ServiceBase<TRequest>
where TRequest : BaseAggregationRequest
where TResponse : BaseAggregationResponse
{
....
protected override object HandleException(TRequest request, Exception ex)
{
base.HandleException(request, ex);
Logger.Error(request.Metadata, ex);
var response = (TResponse) Activator.CreateInstance(typeof (TResponse), new Metadata());
response.Exception = new AggregationException(ex);
return response;
}
}
下面是实现的本机示例(来自github wiki)。
public class MyServiceBase<TRequest> : RestService<TRequest>
{
public override object HandleException(TRequest request, Exception exception)
{
//log your exceptions here
...
//call default exception handler or prepare your own custom response with
return base.HandleException(request, exception);
// or prepare new customer response with new HttpError(...)
}
}
我的问题是我无法接收异常字段 - 它始终为空。此方法中的其他更改按预期收到响应。我应该做任何额外的配置还是我没有适当的实施?