WCF肥皂格式

时间:2012-07-04 03:34:27

标签: .net wcf

我正在开发一个将由政府机构使用的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>

现在,我的当前服务的消息,绑定=“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消息

“&lt; SOAP-ENV:Envelope xmlns:SOAP- ...&gt;&lt; SOAP-ENV:Body&gt;”

而不是使用......

&lt; s:Envelope ....&gt; &lt; s:标题/&gt;

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

这是我当前的配置,使用建议的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>

有人提出任何线索吗?谢谢!

3 个答案:

答案 0 :(得分:1)

您需要将WCF服务的绑定更改为使用basicHttpBinding的{​​{1}},而SOAP v1.1使用wsHttpBinding

请参阅此处:SOAP 1.1 vs SOAP 1.2

您还可以创建自定义绑定,并明确指定您想要的SOAP v1.2

SOAP 1.1

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

[MessageContract]是简单类,它们还用作传输的SOAP数据包。

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