WCF Soap格式

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>

现在,我的当前服务的消息,绑定名为“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:Envelope xmlns:SOAP- ... > <SOAP-ENV:Body>"

而不是使用...

<s:Envelope .... > <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>

有什么线索吗?谢谢!
3个回答

1
您可以使用自定义消息编码器来格式化消息,使其完全符合您的需求。这是一个关于如何实现它的URL链接: http://msdn.microsoft.com/en-us/library/ms751486.aspx
在WriteMessage方法中,您可以进行所需的更改,以进行消息处理。

这是一个残酷的答案,但看起来是正确的。为WCF创建自定义格式并不容易! - Paul Keister

1

您需要更改WCF服务的绑定为basicHttpBinding,它使用SOAP v1.1,而wsHttpBinding使用SOAP v1.2

请参见此处:SOAP 1.1 vs SOAP 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>  

谢谢,我尝试过basicHttpBinding,就如我上面所述,但是basicHttpBinding的默认输出格式与我发布的第二组Request/Response格式相同。我需要输出格式像第一组那样。 - Modesto
@Modesto:你能发布一下使用 basicHttpBinding 的响应/请求对是什么样子的吗? - Marcel N.
我刚刚尝试了您提供的customBinding(在主机和客户端中都使用)。但是我得到了相同的错误信息。 - Modesto
@Modesto:但这不可能。您能否更新您的问题,包括服务配置(绑定、服务和端点)?一定是出了什么问题。 - Marcel N.
顺便说一句,到目前为止,我只能使用 .ASMX 服务重新创建所需的格式,但无法使用 WCF(我不知道是否可能)。 - Modesto

0

如果您想要完全控制 WCF 中的响应,您需要使用 [MessageContract]。

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

根据您的需求,您需要使用 MessageContract。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接