【问题标题】:WCF and Input Parameter Order in SOAP EnvelopeSOAP 信封中的 WCF 和输入参数顺序
【发布时间】:2012-07-26 10:56:27
【问题描述】:

我在使用 webHttpBinding (soap 1.1) 的 WCF Web 服务中收到未设置为对象错误实例的对象引用我注意到,如果您按特定顺序输入参数,则不会引发错误.

即

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:not="http://schemas.globalfoundries.com/NotificationService">
   <soapenv:Header/>
   <soapenv:Body>
      <not:NotifyWorkflowItemUpdate>
         <not:userIDs>testUserID</not:userIDs>
         <not:taskID>testTaskID</not:taskID>
         <not:taskType>testTaskType</not:taskType>
         <not:status>testStatus</not:status>
         <not:appID>testAppID</not:appID>
         <not:message>testMessage</not:message>
      </not:NotifyWorkflowItemUpdate>
   </soapenv:Body>
</soapenv:Envelope>

但是,如果我更改请求模板中输入参数的顺序,则会出现上述错误。即(注意消息和用户ID参数切换)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:not="http://schemas.globalfoundries.com/NotificationService">
   <soapenv:Header/>
   <soapenv:Body>
      <not:NotifyWorkflowItemUpdate>
     <not:message>testMessage</not:message>
         <not:taskID>testTaskID</not:taskID>
         <not:taskType>testTaskType</not:taskType>
         <not:status>testStatus</not:status>
         <not:appID>testAppID</not:appID>
         <not:userIDs>testUserID</not:userIDs>
      </not:NotifyWorkflowItemUpdate>
   </soapenv:Body>
</soapenv:Envelope>

为什么会这样?请求参数是否通过顺序而不是名称映射到 .Net 方法参数?是否必须在服务合同上指定一个属性才能使命名参数映射成为可能?

【问题讨论】:

    标签: wcf soap request input-parameters


    【解决方案1】:

    您需要在 WCF 服务接口中使用 XmlSerializerFormat 类。

    [ServiceContract, XmlSerializerFormat]
    public interface IGoodMessageService
    {
        ...
    }
    

    问题及解决方法在此链接中说明:http://neimke.blogspot.com.tr/2012/03/serialization-ordering-causes-problems.html

    【讨论】:

    • 是的,如果可以的话,请添加一些解释,我会接受这个作为答案。不过等了两年才得到答案:D
    【解决方案2】:

    您的 SOAP 消息的 XML 模式指定了顺序。在 XML 中,元素的顺序很重要,WCF 正在根据架构验证 XML。

    【讨论】:

    • 有没有办法使用命名参数之类的东西,只要标签中指定了正确的名称,消费客户端就可以按任何顺序指定参数?
    • 我不这么认为。 SOAP 和 XSD 是为互操作性而设计的明确定义的标准。不要将 SOAP 视为方法和参数,因为您的语言会生成代理。将其视为消息传递,并且该消息的合同经过严格验证。
    • 我刚刚发布了同样的问题,只是我在 ASMX 中添加了这一点,排序无关紧要。我认为 WCF 中有一个设置可以让您模仿 ASMX 中的无序功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多