当前位置:  开发笔记 > Android > 正文

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

如何解决《.NETWCF错误生成不正确的SOAP1.1错误代码值》经验,为你挑选了2个好方法。

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

仅供参考:使用带有wsHttpBinding的FaultExceptions会导致SOAP 1.2语义,而使用带有basicHttpBinding的FaultExceptions会导致SOAP 1.1语义.

我使用以下代码抛出FaultException :

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

FaultDetails类只是一个简单的测试类,它包含一个字符串"Message"属性,如下所示.

使用wsHttpBinding时,响应是:




  Sender
  
    MySubFaultCode
  


  Testing fault exceptions.


  
    Throwing FaultException<FaultDetails>.
  

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

使用basicHttpBinding时,响应是:



  s:MySubFaultCode
  Testing fault exceptions.
  
    
      Throwing FaultException<FaultDetails>.
    
  

这看起来不对.看一下SOAP 1.1规范,当我使用FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode"))时,我希望看到"faultcode"的值为"s:Client.MySubFaultCode".此外,WCF客户端的结构也不正确.faultException.Code.Name是"MySubFaultCode"而不是"Sender",而faultException.Code.SubCode是null而不是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?反馈ID = 367963

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

我做错了什么,或者这是WCF的真正错误吗?



1> wojo..:

这是我目前的解决方法:

    /// 
    /// 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.
    /// 
    /// 
    /// See http://stackoverflow.com/questions/65008/net-wcf-faults-generating-incorrect-soap-11-faultcode-values
    /// for more details.
    /// 
    public static class FaultCodeFactory
    {
        private const string _ns = "http://schemas.microsoft.com/ws/2005/05/envelope/none";

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

        /// 
        /// Creates a receiver fault code.
        /// 
        /// A FaultCode object.
        /// Does not support subcodes due to a WCF bug.
        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中工作.



2> wojo..:

来自Microsoft的回复:

如http://msdn.microsoft.com/en-us/library/ms789039.aspx中所述,Soap 1.1规范中针对自定义错误代码概述了两种方法:

(1)使用您描述的"点"符号

(2)定义全新的故障代码

不幸的是,应该避免使用"点"表示法,因为在WS-I Basic Profile规范中不鼓励使用它.从本质上讲,这意味着在使用Soap 1.1时,没有真正等效的Soap 1.2故障SubCode.

因此,在生成错误时,您必须认识到绑定中定义的MessageVersion,并相应地生成错误代码.

由于"发送器"和"接收器"不是Soap 1.1的故障代码,并且没有真正等效的故障子代码,因此在为Soap 1.1生成自定义故障代码时,不应使用CreateSenderFaultCode和CreateReceiverFaultCode方法.

相反,您需要使用自己的命名空间和名称来定义自己的错误代码:

FaultCode customFaultCode = new FaultCode(localName,faultNamespace);

推荐阅读
小白也坚强_177
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有