3

我正在开发一个将由政府机构使用的 WCF 服务。由于他们将访问由不同承包公司托管的相同服务,因此他们指定了消息的确切外观(因此,他们只会更改 URL 以访问每个公司的服务)。

所以,对于这个方法:

sbyte Execute(int Id1, short Id2, string Id3, string Id4, string Value)

他们希望我们完全接受以下 SOAP 消息:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http:/
/www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/
XMLSchema">
    <SOAP-ENV:Body>
        <m:Execute xmlns:m="http://tempuri.org/">
            <m:Id1>1</m:Id1>
            <m:Id2>2</m:Id2>
            <m:Id3>3</m:Id3>
            <m:Id4>4</m:Id4>
            <m:Value></m:Value>
        </m:Execute>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我们的回应必须是:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <ExecuteResponse xmlns="http://tempuri.org/">
            <ExecuteResult>
                <res>0</res>
            </ExecuteResult>
        </ExecuteResponse>
    </soap:Body>
</soap:Envelope>

现在,我当前服务的消息,使用 binding="basicHttpBinding" 并使用我的测试客户端(以及 WCF 测试客户端工具),如下所示:

要求:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ISomeService/Execute</Action>
  </s:Header>
  <s:Body>
    <Execute xmlns="http://tempuri.org/">
      <Id1>0</Id1>
      <Id2>0</Id2>
      <Id3>0</Id3>
      <Id4>0</Id4>
      <Value>0</Value>
    </Execute>
  </s:Body>
</s:Envelope>

回复:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <ExecuteResponse xmlns="http://tempuri.org/">
      <ExecuteResult>1</ExecuteResult>
    </ExecuteResponse>
  </s:Body>
</s:Envelope>


所以,我的问题是:我应该改变什么,让我的服务使用 SOAP 消息,如

“<SOAP-ENV:信封 xmlns:SOAP- ... > <SOAP-ENV:Body>”

而不是使用...

<s:信封 .... > <s:Header />

我尝试了几种绑定、自定义配置等,但我无法让它看起来一样。

这是我当前的配置,使用建议的 customBinding:

  <system.serviceModel>
    <services>
      <service name="Test.SomeService">
        <endpoint binding="customBinding" bindingConfiguration="Soap11Binding"
          contract="Test.ISomeService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <customBinding>
        <binding name="Soap11Binding">
          <textMessageEncoding messageVersion="Soap11" writeEncoding="utf-8">
            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          </textMessageEncoding>
          <httpTransport allowCookies="true" />
        </binding>
      </customBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

任何线索任何人?谢谢!

4

3 回答 3

1

您需要将 WCF 服务的绑定更改为basicHttpBinding, which uses SOAP v1.1, while wsHttpBindinguses SOAP v1.2

见这里:SOAP 1.1vsSOAP 1.2

您还可以创建自定义绑定并明确指定您想要的SOAP 1.1

<customBinding>  
    <binding name="Soap11Binding">  
        <textMessageEncoding messageVersion="Soap11" writeEncoding="utf-8">  
            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"  
                maxBytesPerRead="4096" maxNameTableCharCount="16384" />  
        </textMessageEncoding>  
        <httpTransport allowCookies="true" />  
    </binding>  
</customBinding>  
于 2012-07-04T05:34:34.840 回答
1

您可以使用自定义消息编码器将消息格式化为您需要的方式。这是如何实现它的 URL:http: //msdn.microsoft.com/en-us/library/ms751486.aspx

在 WriteMessage 方法中,您可以对消息进行所需的更改。

于 2012-07-05T07:44:27.167 回答
0

要完全控制 wcf 中的响应,您需要使用 [MessageContract]。

[MessageContract] 是简单的类,进一步用作 SOAP 数据包进行传输。

通过考虑您的要求,您需要使用 MessageContract。

于 2014-07-15T13:10:22.783 回答