.NET WCF出现错误的SOAP 1.1错误代码值

前端之家收集整理的这篇文章主要介绍了.NET WCF出现错误的SOAP 1.1错误代码值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用FaultException和FaultException< T>以确定我们应用程序中的最佳使用模式.我们需要支持WCF以及非WCF服务使用者/客户端,包括SOAP 1.1和SOAP 1.2客户端.

FYI:使用wsHttpBinding的FaultExceptions产生SOAP 1.2语义,而使用带有basicHttpBinding的FaultExceptions则产生SOAP 1.1语义.

我使用以下代码来抛出一个FaultException< FaultDetails&gt ;::

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规范看起来正确.主/根“代码”是“发件人”,它具有“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(新的FaultCode(“MySubFaultCode”))时,我期望看到“faultcode”的值为“s:Client.MySubFaultCode”. WCF客户端也会获得不正确的结构. faultException.Code.Name是“MySubFaultCode”而不是“发件人”,而faultException.Code.SubCode为空,而不是faultException.Code.SubCode.Name为“MySubFaultCode”.此外,faultException.Code.IsSenderFault为false.

使用FaultCode.CreateReceiverFaultCode(新的FaultCode(“MySubFaultCode”))时出现类似的问题)

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

这个项目也是2006年的其他人在http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=669420&SiteID=1发布的,没有人回答.我觉得很难相信没有人遇到这个问题.

这里有其他人有类似的问题: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中的一个bug吗?

解决方法

这是我目前的解决方法
/// <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 https://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兼容的faultcode值,但是它会破坏SOAP 1.2.

如果您在.NET中使用适当的子代码支持(通过静态FaultCode方法或其中一种重载),则会破坏SOAP 1.1,但在SOAP 1.2中起作用.

原文链接:https://www.f2er.com/html/224137.html

猜你在找的HTML相关文章