.NET WCF 错误生成不正确的 SOAP 1.1 错误代码值

标签 .net wcf web-services soap

我正在尝试使用FaultException 和FaultException 来确定应用程序中的最佳使用模式。我们需要支持 WCF 以及非 WCF 服务使用者/客户端,包括 SOAP 1.1 和 SOAP 1.2 客户端。

仅供引用:将FaultExceptions 与wsHttpBinding 结合使用会产生SOAP 1.2 语义,而将FaultExceptions 与basicHttpBinding 结合使用会产生SOAP 1.1 语义。

我使用以下代码抛出FaultException:

  throw new FaultException<FaultDetails>(
      new FaultDetails("Throwing FaultException<FaultDetails>."),
      new FaultReason("Testing fault exceptions."),
      FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode"))
      );

FaultDetails 类只是一个简单的测试类,其中包含一个字符串“Message”属性,如下所示。

当使用 wsHttpBinding 时,响应是:

<?xml version="1.0" encoding="utf-16"?>
<Fault xmlns="http://www.w3.org/2003/05/soap-envelope">
<Code>
  <Value>Sender</Value>
  <Subcode>
    <Value>MySubFaultCode</Value>
  </Subcode>
</Code>
<Reason>
  <Text xml:lang="en-US">Testing fault exceptions.</Text>
</Reason>
<Detail>
  <FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Message>Throwing FaultException&lt;FaultDetails&gt;.</Message>
  </FaultDetails>
</Detail>

根据 SOAP 1.2 规范,这看起来是正确的。主/根“代码”是“Sender”,其“子代码”为“MySubFaultCode”。如果服务使用者/客户端使用 WCF,则客户端的FaultException 也会模仿相同的结构,其中faultException.Code.Name 为“Sender”,faultException.Code.SubCode.Name 为“MySubFaultCode”。

使用 basicHttpBinding 时,响应为:

<?xml version="1.0" encoding="utf-16"?>
<s:Fault xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <faultcode>s:MySubFaultCode</faultcode>
  <faultstring xml:lang="en-US">Testing fault exceptions.</faultstring>
  <detail>
    <FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <Message>Throwing FaultException&lt;FaultDetails&gt;.</Message>
    </FaultDetails>
  </detail>
</s:Fault>

这看起来不对。查看 SOAP 1.1 规范,当我使用FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode")) 时,我期望看到“faultcode”的值为“s:Client.MySubFaultCode”。 WCF 客户端也会得到不正确的结构。 failureException.Code.Name 是“MySubFaultCode”而不是“Sender”,faultException.Code.SubCode 是 null,而不是 failureException.Code.SubCode.Name 是“MySubFaultCode”。另外,faultException.Code.IsSenderFault 为 false。

使用FaultCode.CreateReceiverFaultCode(new FaultCode("MySubFaultCode"))时出现类似问题:

  • 按 SOAP 1.2 的预期工作
  • 生成“s:MySubFaultCode”而不是“s:Server.MySubFaultCode”,并且对于 SOAP 1.1,faultException.Code.IsReceiverFault 为 false

此项目也由其他人发布在http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=669420&SiteID=1上2006年就没有人回答过这个问题。我很难相信还没有人遇到过这种情况。

这是其他人也遇到类似问题:http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3883110&SiteID=1&mode=1

Microsoft Connect 错误:https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=367963

故障如何工作的描述:http://blogs.msdn.com/drnick/archive/2006/12/19/creating-faults-part-3.aspx

我做错了什么还是这确实是 WCF 中的错误?

最佳答案

这是我当前的解决方法:

    /// <summary>
    /// Replacement for the static methods on FaultCode to generate Sender and Receiver fault codes due
    /// to what seems like bugs in the implementation for basicHttpBinding (SOAP 1.1). wsHttpBinding 
    /// (SOAP 1.2) seems to work just fine.
    /// 
    /// The subCode parameter for FaultCode.CreateReceiverFaultCode and FaultCode.CreateSenderFaultCode
    /// seem to take over the main 'faultcode' value in the SOAP 1.1 response, whereas in SOAP 1.2 the
    /// subCode is correctly put under the 'Code->SubCode->Value' value in the XML response.
    /// 
    /// This workaround is to create the FaultCode with Sender/Receiver (SOAP 1.2 terms, but gets
    /// translated by WCF depending on the binding) and an agnostic namespace found by using reflector
    /// on the FaultCode class. When that NS is passed in WCF seems to be able to generate the proper
    /// response with SOAP 1.1 (Client/Server) and SOAP 1.2 (Sender/Receiver) fault codes automatically.
    /// 
    /// This means that it is not possible to create a FaultCode that works in both bindings with
    /// subcodes.
    /// </summary>
    /// <remarks>
    /// See http://stackoverflow.com/questions/65008/net-wcf-faults-generating-incorrect-soap-11-faultcode-values
    /// for more details.
    /// </remarks>
    public static class FaultCodeFactory
    {
        private const string _ns = "http://schemas.microsoft.com/ws/2005/05/envelope/none";

        /// <summary>
        /// Creates a sender fault code.
        /// </summary>
        /// <returns>A FaultCode object.</returns>
        /// <remarks>Does not support subcodes due to a WCF bug.</remarks>
        public static FaultCode CreateSenderFaultCode()
        {
            return new FaultCode("Sender", _ns);
        }

        /// <summary>
        /// Creates a receiver fault code.
        /// </summary>
        /// <returns>A FaultCode object.</returns>
        /// <remarks>Does not support subcodes due to a WCF bug.</remarks>
        public static FaultCode CreateReceiverFaultCode()
        {
            return new FaultCode("Receiver", _ns);
        }
    }

遗憾的是,我没有找到一种在不破坏 SOAP 1.1 或 1.2 客户端的情况下使用子代码的方法。

如果您使用 Code.SubCode 语法,则可以创建 SOAP 1.1 兼容的错误代码值,但它会破坏 SOAP 1.2。

如果您在.NET 中使用正确的子代码支持(通过静态FaultCode 方法或重载之一),它会破坏 SOAP 1.1,但可以在 SOAP 1.2 中工作。

关于.NET WCF 错误生成不正确的 SOAP 1.1 错误代码值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65008/

相关文章:

c# - .NET 日志记录类不写

c# - 对象属性在 Web 表单上返回零而不是 null

.net - Windows Communication Foundation (WCF) 可以被认为是中间件技术

asp.net - WCF Web 服务错误 : The service cannot be activated because it does not support ASP. NET 兼容性

c# - 如何将我的 Silverlight 应用程序连接到 WCF 服务?

c# - 无法建立连接,因为目标机器主动拒绝了它-套接字C#

.net - 是否可以忽略 Visual Studio 中某些行的某些异常?

c# - 如何将 lambda 表达式传递给 WCF 服务?

java - Axis2 Web 服务客户端应用程序

java - 如何编写接受二进制文件的 Restful 网络服务 (pdf)