将WSE示例代码转换为WCF

时间:2009-04-22 18:59:28

标签: .net web-services wcf wse

我是WSE和WCF的新手,我正在尝试使用WCF使用Web服务,但所有示例文档都适用于VS2005 + WSE。此Web服务使用WS-Security 1.0。我通过visual studio添加了一个服务引用,但我对如何在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);

1 个答案:

答案 0 :(得分: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"
    
  4. TransportWithMessageCredential等同于WSE 3.0下的UsernameOverTransportAssertion策略