我有一个定制的IErrorHandler一个REST WCF服务,使我能赶上我在服务中的所有捕获的异常,并返回一个自定义错误消息,正确的HTTP状态代码(500),并记录错误。REST WCF服务与IErrorHandler抓住SerializationExceptions
问题是,IErrorHandler将捕获不是源自我的代码的异常,所以如果我例如对具有无效JSON数据的服务进行POST,我将得到一个SerializationException。如果不是我的IErrorHandler,那么这个异常将被转换为状态码为BadRequest 400的WebFaultException,我将像处理所有其他未捕获的异常一样处理它。
有没有办法处理这些情况,或者我应该在我的IErrorHandler中捕获SerializationException并在那里设置BadRequest?如果没有来自我的代码的其他异常可能来自WCF堆栈的其他异常?
更新:新增IErrorHandler.ProvideFault
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
Guid loggingId = Guid.NewGuid();
error.Data["ExceptionLoggingId"] = loggingId;
if (error is SecurityTokenException)
{
fault = Message.CreateMessage(version, string.Empty, String.Format("{0}. The error identifier is {1}", error.Message, loggingId), new DataContractJsonSerializer(typeof(string)));
fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json));
webOperationContextWrapper.SetOutgoingResponseStatusCode(HttpStatusCode.Unauthorized);
}
else
{
if (error is SerializationException)
{
// TODO: What if the SerializationException originates from within the service?
// SerializationException due to malformed JSON
return;
}
fault = Message.CreateMessage(version, string.Empty, String.Format("An unknown error has occurred. The error identifier is {0}", loggingId), new DataContractJsonSerializer(typeof(string)));
fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json));
webOperationContextWrapper.SetOutgoingResponseStatusCode(HttpStatusCode.InternalServerError);
}
}
如果您在ProvideFault中未分配故障消息,会发生什么情况? –
然后它按预期工作;客户端将收到http状态码400.但是,这意味着我要检查IErrorHandler中的SerializationExceptions。但是,如果SerializationException是从我的服务而不是来自WCF的堆栈,那么它是一个服务器错误(有些系列化我做了错误),我想返回状态代码500。你看这个问题? – Joel
我说我的实现澄清我的意思 – Joel