将安全绑定从配置文件转换为编码

时间:2013-02-09 05:26:28

标签: c# .net wcf wcf-binding

想要调用HTTPS WCF服务。当我们这样打电话时:

SmartPayments2SoapClient client = new SmartPayments2SoapClient();

它正常工作。

但在我的场景中,我不想使用默认绑定。它必须以编程方式创建,但在此,我的服务是安全的,意味着它在HTTPS上运行。

请建议我可以用编程方式创建绑定的代码。

以下是带有自定义绑定的App.config:

<customBinding>
    <binding name="SmartPayments2Soap12">
        <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8">
            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        </textMessageEncoding>
        <httpsTransport manualAddressing="false" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" requireClientCertificate="false" />
    </binding>
</customBinding>

我希望在C#程序中进行转换。我试过这个:

SecurityBindingElement securityElement = SecurityBindingElement.CreateSslNegotiationBindingElement(false);
HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();
httpsTransport.AllowCookies = false;
httpsTransport.BypassProxyOnLocal = false;
httpsTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
httpsTransport.MaxBufferSize = 65536;
httpsTransport.MaxBufferPoolSize = 524288;
httpsTransport.MaxReceivedMessageSize = 65536;
httpsTransport.TransferMode = TransferMode.Buffered;
httpsTransport.UseDefaultWebProxy = true;
httpsTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
httpsTransport.ManualAddressing = false;
httpsTransport.AllowCookies = false;
httpsTransport.BypassProxyOnLocal = false;
//httpsTransport.DecompressionEnabled = true;
httpsTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
httpsTransport.KeepAliveEnabled = true;
httpsTransport.ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
httpsTransport.Realm = "";
httpsTransport.TransferMode = TransferMode.Buffered;
httpsTransport.UnsafeConnectionNtlmAuthentication = false;
httpsTransport.UseDefaultWebProxy = true;

CustomBinding binding = new CustomBinding(securityElement, httpsTransport);

EndpointAddress remoteAddress = new EndpointAddress("https://example.com/Payments/Transact2.asmx");

SmartPayments2SoapClient client = new SmartPayments2SoapClient(binding, remoteAddress);

0 个答案:

没有答案
相关问题