WCF IErrorHandler 将 FaultException 返回给 SOAP,将 WebHttpException 返回给 POX 和 Json 端点

标签 wcf rest soap

我有一组使用 IErrorHandler 包装异常的 SOAP Web 服务,特别是:

public sealed class ErrorHandler : IErrorHandler
{
    public bool HandleError(Exception error)
    {
        return true;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        // don't wrap existing fault exceptions
        if ((error is FaultException)) return;

        // our basic service fault
        var businessFault = new BusinessFault { FaultMessage = error.Message, FaultReference = "Internal" };

        // Resource based faultReason
        var faultReason = new FaultReason(Properties.Resources.BusinessFaultReason);
        var faultcode = FaultCodeFactory.CreateVersionAwareSenderFaultCode(InternalFaultCodes.BusinessFailure.ToString(), Service.Namespace);

        var faultException = new FaultException<BusinessFault>(
            businessFault,
            faultReason,
            faultcode);

        // Create message fault
        var messageFault = faultException.CreateMessageFault();

        // Create message using Message Factory method
        fault = Message.CreateMessage(version, messageFault, faultException.Action);
    }
}

我现在为 Json 和 Pox 添加了额外的端点,它们可以正常工作,除非发生异常。在 Json 端点的情况下,FaultException 作为 XML 返回。

我从其他 SO 帖子中知道,在 REST 的情况下,我最好抛出 WebHttpException:
throw new WebFaultException<BusinessFault>(detail, HttpStatusCode.BadRequest);

或者覆盖 ProvideFault 中的响应消息属性,因此:
var wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);
fault.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);

var rmp = new HttpResponseMessageProperty
{
    StatusCode = System.Net.HttpStatusCode.BadRequest,
    StatusDescription = "See fault object for more information."
};
fault.Properties.Add(HttpResponseMessageProperty.Name, rmp);

但是,MSDN 有一些关于 WebHttpException 的有趣评论。即:

When using a WCF REST endpoint (WebHttpBinding and WebHttpBehavior or WebScriptEnablingBehavior) the HTTP status code on the response is set accordingly. However, WebFaultException can be used with non-REST endpoints and behaves like a regular FaultException.

When using a WCF REST endpoint, the response format of the serialized fault is determined in the same way as a non-fault response. For more information about WCF REST formatting, see WCF REST Formatting.



因此,它建议我需要转换我当前的 ProvideFault 方法以提供新的 WebHttpException(包装任何现有的异常或故障异常),然后 SOAP 仍然可以正常工作。

有人想试一试那会是什么样子(.Net4.0 btw)吗?我想要一个错误处理程序来统治它们!

最佳答案

我的印象是使用 webHttpBinding是一种获得 JSON/POX/SOAP 的“一体化”功能的方法,而不是为每个使用单独的绑定(bind)(即 wsHttpBindingbasicHttpBinding 等)。所以你不能扔WebHttpException吗?然后不管技术如何,都能为您提供所需的所有错误详细信息?

关于WCF IErrorHandler 将 FaultException 返回给 SOAP,将 WebHttpException 返回给 POX 和 Json 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7188519/

相关文章:

php - 如何创建 SOAP 1.2 请求

java - SOAP、WCF 和消息签名

c# - 将 WCF 服务限制为特定用户帐户的最佳方法

c# - 在Azure上部署WCF服务,尝试访问数据库时超时

java - oauth2 的 Spring Security 向授权 URL 添加附加参数?

.net - WCF Service Trace Viewer 工具是不可或缺的吗?

c# - 使用 Web 服务时类型不兼容

c# - 具有 'normal' 接口(interface)的 WCF 异步实现

java - 为什么当我在存储库中保存对象时相同的 id 会递增?

java - 当函数没有返回值时,Delphi XE2 是否正确从 Java/Axis2 导入 WSDL?