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

.NET WCF出现错误的SOAP 1.1错误代码值,第1张

概述我正在尝试使用FaultException和FaultException< T>以确定我们应用程序中的最佳使用模式.我们需要支持WCF以及非WCF服务使用者/客户端,包括SOAP 1.1和SOAP 1.2客户端. FYI:使用wsHttpBinding的FaultExceptions产生SOAP 1.2语义,而使用带有basicHttpBinding的FaultExceptions则产生SOAP 我正在尝试使用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中起作用.

总结

以上是内存溢出为你收集整理的.NET WCF出现错误的SOAP 1.1错误代码值全部内容,希望文章能够帮你解决.NET WCF出现错误的SOAP 1.1错误代码值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144052.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存