在WCF中操纵肥皂信封

时间:2015-06-10 08:47:53

标签: c# wcf soap

对于一个项目,我们正在连接荷兰的KVK Handelsregister DataService。我们已经设置了一个WCF客户端代理类,但是我们的soap信封仍然与它们请求的信封不同,任何想法都可以解决这三个问题:

  1. 删除mustUnderstandAttribute
  2. 正文中没有名称空间(ns:)
  3. 标题也没有对OASIS的引用
  4. 我们的信封:

    <s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
      <s:Header>
        <a:Action s:mustUnderstand="1">http://es.kvk.nl/genereerProduct</a:Action>
        <a:MessageID>urn:uuid:173035b3-714d-46ef-96c2-1d46962ec897</a:MessageID>
        <ActivityId CorrelationId="3d2fb1c7-99db-4c88-9745-6cd187281fc6" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">5f48b058-21d5-46c4-8a8b-02f60556ef0e</ActivityId>
        <a:ReplyTo>
          <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <a:To s:mustUnderstand="1">http://es.kvk.nl/KVK-DataserviceCT/2012/10</a:To>
      </s:Header>
      <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <genereerProduct xmlns="http://schemas.kvk.nl/schemas/hrip/generiekproduct/2013/01">
          <productRequest>
            <klantreferentie>QW43BA12</klantreferentie>
            <productnaam>Inschrijving</productnaam>
            <productsleutel>
              <kvkNummer>01048900</kvkNummer>
            </productsleutel>
          </productRequest>
        </genereerProduct>
      </s:Body>
    </s:Envelope>
    

    信封应该是

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
    xmlns:ns="http://schemas.kvk.nl/schemas/hrip/generiekproduct/2013/01" 
    xmlns:add="http://www.w3.org/2005/08/addressing">
                <soapenv:Header>
                            <add:Action>http://es.kvk.nl/genereerProduct</add:Action>
                            <add:MessageID>uuid://94-b0e0-d4313c898af0</add:MessageID>
                            <add:To>http://es.kvk.nl/KVK-DataserviceCT/2012/10</add:To>
                </soapenv:Header>
                <soapenv:Body>
                            <ns:genereerProduct>
                                        <ns:productRequest>
                                                    <ns:klantreferentie>QW43BA12</ns:klantreferentie>
                                                    <ns:productnaam>Inschrijving</ns:productnaam>
                                                    <ns:productsleutel>
                                                                <ns:kvkNummer>01048900</ns:kvkNummer>
                                                    </ns:productsleutel>
                                        </ns:productRequest>
                            </ns:genereerProduct>
                </soapenv:Body>
    </soapenv:Envelope>*
    

    根据评论中的请求,wsHttpBinding和customBinding的绑定配置。更改此信息对soap信封没有影响,自定义绑定的错误是:

      

    错误接收HTTP响应时发生错误   https://webservices   8.kvk.nl/。这可能是由于服务端点绑定不使用HTTP协议。这也可能是由HTTP请求上下文引起的   被服务器中止(可能是由于服务关闭)   下)。有关更多详细信息,请参阅服务器日志。

    <bindings>
      <wsHttpBinding>
        <binding name="ProductServiceSoap11">
          <security mode="Transport">
            <message clientCredentialType="Certificate"/>
            <transport clientCredentialType="Certificate" />
    
          </security>
        </binding>
      </wsHttpBinding>
      <customBinding>
        <binding name="WsHttpSoap11" >
          <transactionFlow />
          <textMessageEncoding messageVersion="Soap11WSAddressing10"  />
          <httpsTransport requireClientCertificate="true"  />
    
        </binding>
      </customBinding>
    </bindings>
    

1 个答案:

答案 0 :(得分:0)

至少我找到了正确的配置并使此链接正常工作。毕竟摆弄SOAP信封只是整个解决方案的一部分。

但我会添加一些信息,这对我的工作有很大帮助。

1)记录,将其添加到配置文件以进行记录

<system.diagnostics>
<sources>
  <source name="System.ServiceModel" switchValue="Information,ActivityTracing"
    propagateActivity="true">
    <listeners>
      <add name="xml" />
    </listeners>
  </source>
  <source name="System.ServiceModel.MessageLogging">
    <listeners>
      <add name="xml" />
    </listeners>
  </source>
</sources>
<sharedListeners>
  <add initializeData="c:\\TestKVKService\TestKVKService\bin\Debug\Log\Tracesmore.svclog" type="System.Diagnostics.XmlWriterTraceListener"
    name="xml" />
</sharedListeners>
<trace autoflush="true" />

2)并将其添加到serviceModel标记中。请务必启用所有消息,当然 logMessagesAtTransportLevel ,因为这样您就会看到通过网络发送消息的方式。

 <system.serviceModel>
<diagnostics>
  <messageLogging
       logEntireMessage="true"
       logMalformedMessages="true"
       logMessagesAtServiceLevel="true"
       logMessagesAtTransportLevel="true"
       maxMessagesToLog="3000"
       maxSizeOfMessageToLog="64000"/>
</diagnostics>

3)最终的配置文件看起来像这样

  • MutualCertificate :因为邮件在发送之前已经过签名,并在收到邮件时签名,但有2个不同的凭据,但这是 在端点biding
  • 中指定
  • messageProtectionOrder :使wcf加密并签名消息
  • defaultAlgorithmSuite :因为.NET使用的是除此java服务器之外的其他算法
  • allowSerializedSigningTokenOnReply :因为响应也已签名
  • requireClientCertificate :将客户端证书发送到服务器
  • ExInspector :需要在信封中操纵TO
  • clientVia :因为它正在与中间路由器通信

最后但不是租约:这个服务器正在向我发回空行动,所以我不得不摆弄生成的代理类。

     <bindings>
  <customBinding>
    <binding name="WsHttpSoap11" >
      <transactionFlow />
      <textMessageEncoding messageVersion="Soap11WSAddressing10"  />
      <security authenticationMode="MutualCertificate" 
                messageSecurityVersion="WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10"
                 messageProtectionOrder="EncryptBeforeSign"
                defaultAlgorithmSuite="TripleDesRsa15" 
                allowSerializedSigningTokenOnReply="true" >
      </security>
      <httpsTransport requireClientCertificate="true"   />
    </binding>
  </customBinding>
</bindings>
<client>
  <endpoint address="http://other-endpoint-then-servername" binding="customBinding"
          bindingConfiguration="WsHttpSoap11" contract="ProductService"
          name="ProductServiceSoap11" behaviorConfiguration="myServiceBehaviour">
    <identity>
      <dns value="serverdnsname" />
    </identity>
  </endpoint>
</client>
<extensions>
  <behaviorExtensions>
    <add name="ExInspector" type="TestService.ExClientBehaviorExtensionElement, TestService"/>
  </behaviorExtensions>
</extensions>
<behaviors>
  <endpointBehaviors>
    <behavior name="myServiceBehaviour">
      <clientCredentials>
        <clientCertificate findValue="clientcertificatename" storeLocation="CurrentUser" x509FindType="FindBySubjectName" storeName="My" />
        <serviceCertificate >
          <defaultCertificate storeLocation="CurrentUser" storeName="My" findValue="servercertificatename" x509FindType="FindBySubjectName"  />
        </serviceCertificate>
      </clientCredentials>
      <clientVia viaUri="https://serverdnsname" />
      <ExInspector />
    </behavior>
  </endpointBehaviors>
</behaviors>

如果对该主题有任何疑问,请随时与我联系。通过这个博客,它对我帮助很大:http://webservices20.blogspot.be/2012/06/12-common-wcf-interop-confusions.html