2009-04-22 75 views
5

我是WSE和WCF的新手,我試圖使用WCF使用Web服務,但所有示例文檔都是針對VS2005 + WSE。此Web服務使用WS-Security 1.0。我已經添加了通過Visual Studio的服務引用,但我就如何在WCF做下面的代碼相當於損失:將WSE示例代碼轉換爲WCF

// 1. Initialize the web service proxy 
PartnerAPIWse integrationFramework = new PartnerAPIWse(); 

// 2. Set the username/password. This is using the Username token of WS-Security 1.0 
UsernameTokenProvider utp = new UsernameTokenProvider("username", "password"); 
integrationFramework.SetClientCredential<UsernameToken>(utp.GetToken()); 

// 3. Declare the policy 
Policy policy = new Policy(new UsernameOverTransportAssertion()); 
integrationFramework.SetPolicy(policy); 
+0

UsernameOverTransportAssertion政策供將來參考:WSE已過時。所有新的Web服務開發都應該使用WCF完成,並且WSE代碼應儘快退役。 – 2009-09-01 02:33:36

回答

8

花一天做一些實驗後,我想出如何把這段代碼轉換。關鍵是在VS2008正確設置的WCF代理上設置綁定。

  1. 添加服務引用指向WSDL
  2. 打開App.config/Web.config中並找到system.serviceModel部分。將默認soapbinding上的安全模式更改爲TransportWithMessageCredential。下面是我的文件看起來像變更後:

     <basicHttpBinding> 
         <binding name="SoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" 
          receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" 
          bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
          useDefaultWebProxy="true"> 
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
           maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
          <security mode="TransportWithMessageCredential"> 
           <transport clientCredentialType="None" proxyCredentialType="None" 
            realm="" /> 
           <message clientCredentialType="UserName" algorithmSuite="Default" /> 
          </security> 
         </binding> 
        </basicHttpBinding> 
    
  3. 更改上面的示例代碼看起來像這樣

    Dim integrationFramework As New SoapClient() 
    integrationFramework.ClientCredentials.UserName.UserName = "username" 
    integrationFramework.ClientCredentials.UserName.Password = "password" 
    

TransportWithMessageCredential是相當於在WSE 3.0