gpt4 book ai didi

c# - wcf rest ierrorhandler webfaultexception

转载 作者:行者123 更新时间:2023-11-30 17:09:51 24 4
gpt4 key购买 nike

我有一个 WCF 4 Rest 服务,它由一个 java 客户端和一个 c# mvc3 客户端使用。响应内容需要以json形式返回。

我希望能够以一致的方式将任何异常/故障返回给客户。我读到应该使用 WebFaultException 并且确实可以正常工作。

但是,我不想用 WebFaultExceptions 污染我的应用程序逻辑项目,因为它可能被非 Web 项目使用。

因此,我查看了在 WCF Rest 服务中实现 IErrorHandler 以捕获任何异常,然后针对特定异常(即 ValidationException)将异常作为包含在 WebFaultException 中的故障返回。然而,这似乎不起作用,当调用 form fiddler 时,我得到以下 Http 504 响应

[Fiddler] ReadResponse() 失败:服务器未返回对此请求的响应。

我已将代码精简到最基本的部分,以展示我正在做的事情的本质。

public void ProvideFault(Exception error, 
MessageVersion version,
ref Message fault)
{
var ex = new WebFaultException<Exception>(
new Exception("you can't do that"),
HttpStatusCode.Unauthorized);

var xmf = ex.CreateMessageFault();
fault = Message.CreateMessage(version,
null,
xmf,
new DataContractJsonSerializer(xmf.GetType()));
}

我做错了什么?

最佳答案

这是来自 Carlos's blog 的示例代码.它在过去对我有帮助。

public class ValidationAwareErrorHandler : IErrorHandler
{
IErrorHandler originalErrorHandler;
public ValidationAwareErrorHandler(IErrorHandler originalErrorHandler)
{
this.originalErrorHandler = originalErrorHandler;
}

public bool HandleError(Exception error)
{
return error is ValidationException || this.originalErrorHandler.HandleError(error);
}

public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
ValidationException validationException = error as ValidationException;
if (validationException != null)
{
fault = Message.CreateMessage(version, null, new ValidationErrorBodyWriter(validationException));
HttpResponseMessageProperty prop = new HttpResponseMessageProperty();
prop.StatusCode = HttpStatusCode.BadRequest;
prop.Headers[HttpResponseHeader.ContentType] = "application/json; charset=utf-8";
fault.Properties.Add(HttpResponseMessageProperty.Name, prop);
fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json));
}
else
{
this.originalErrorHandler.ProvideFault(error, version, ref fault);
}
}

class ValidationErrorBodyWriter : BodyWriter
{
private ValidationException validationException;
Encoding utf8Encoding = new UTF8Encoding(false);

public ValidationErrorBodyWriter(ValidationException validationException)
: base(true)
{
this.validationException = validationException;
}

protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
writer.WriteStartElement("root");
writer.WriteAttributeString("type", "object");

writer.WriteStartElement("ErrorMessage");
writer.WriteAttributeString("type", "string");
writer.WriteString(this.validationException.ValidationResult.ErrorMessage);
writer.WriteEndElement();

writer.WriteStartElement("MemberNames");
writer.WriteAttributeString("type", "array");
foreach (var member in this.validationException.ValidationResult.MemberNames)
{
writer.WriteStartElement("item");
writer.WriteAttributeString("type", "string");
writer.WriteString(member);
writer.WriteEndElement();
}
writer.WriteEndElement();

writer.WriteEndElement();
}
}
}

关于c# - wcf rest ierrorhandler webfaultexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12457039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com