在同一 WCF 绑定中同时使用 HTTP 和 HTTPS - 在代码中将 <security mode="Transport"> 更改为 <security mode="TransportCredentialOnly">
Using Both HTTP and HTTPS in same WCF Binding - Changing <security mode="Transport"> to <security mode="TransportCredentialOnly"> In Code
我有一个从 Visual Studio 项目中的 WSDL 文件自动生成的绑定,如下所示:
<binding name="XServiceSoap"
maxBufferPoolSize="16777216"
maxBufferSize="16777216"
maxReceivedMessageSize="16777216">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"
proxyCredentialType="None"/>
</security>
</binding>
客户看起来像这样:
<endpoint address="http://uri/XService.asmx"
binding="basicHttpBinding"
bindingConfiguration="XServiceSoap"
contract="XService.XServiceSoap"
name="XServiceSoap"/>
在代码中,我可以使用
轻松更改端点地址(和凭据)
using WPFApp.XService;
var xServiceSoapClient = new XServiceSoapClient("XServiceSoap");
xServiceSoapClient.Endpoint.Address = new EndpointAddress(NEW_ENDPOINT);
xServiceSoapClient
.ClientCredentials.Windows.ClientCredential.UserName = NEW_USERNAME;
xServiceSoapClient
.ClientCredentials.Windows.ClientCredential.Password = NEW_PASSWORD;
但是如果我尝试使用非 http 端点,则会抛出异常 The provided URI scheme 'https' is invalid; expected 'http'.
这个错误对我来说很有意义(例如 from here),但是当我更改端点(测试服务器与生产服务器)时,我希望相同的绑定同时支持 HTTP 和 HTTPS。我如何使用内置的 soap 客户端在代码中执行此操作?我在 xServiceSoapClient
中没有看到更改 security mode
的选项,例如。我没有看到可以更改的 XServiceSoapclient.Security.Mode
选项。
我可以确认,当我手动将 app.config
从 TransportCredentialOnly 更改为 Transport 时,https 连接可以正常工作而不会引发异常。
谢谢!
WCF 客户端的构造函数有一个带有两个参数的重载,binding
和 endpoint
您可以使用此重载来通过所需的安全模式:
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
var xServiceSoapClient = new xServiceSoapClient(binding, "XServiceSoap");
这是一般的想法,使用指定的对象而不是不适用于上述类型操作的服务客户端对象。
我有一个从 Visual Studio 项目中的 WSDL 文件自动生成的绑定,如下所示:
<binding name="XServiceSoap"
maxBufferPoolSize="16777216"
maxBufferSize="16777216"
maxReceivedMessageSize="16777216">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"
proxyCredentialType="None"/>
</security>
</binding>
客户看起来像这样:
<endpoint address="http://uri/XService.asmx"
binding="basicHttpBinding"
bindingConfiguration="XServiceSoap"
contract="XService.XServiceSoap"
name="XServiceSoap"/>
在代码中,我可以使用
轻松更改端点地址(和凭据)using WPFApp.XService;
var xServiceSoapClient = new XServiceSoapClient("XServiceSoap");
xServiceSoapClient.Endpoint.Address = new EndpointAddress(NEW_ENDPOINT);
xServiceSoapClient
.ClientCredentials.Windows.ClientCredential.UserName = NEW_USERNAME;
xServiceSoapClient
.ClientCredentials.Windows.ClientCredential.Password = NEW_PASSWORD;
但是如果我尝试使用非 http 端点,则会抛出异常 The provided URI scheme 'https' is invalid; expected 'http'.
这个错误对我来说很有意义(例如 from here),但是当我更改端点(测试服务器与生产服务器)时,我希望相同的绑定同时支持 HTTP 和 HTTPS。我如何使用内置的 soap 客户端在代码中执行此操作?我在 xServiceSoapClient
中没有看到更改 security mode
的选项,例如。我没有看到可以更改的 XServiceSoapclient.Security.Mode
选项。
我可以确认,当我手动将 app.config
从 TransportCredentialOnly 更改为 Transport 时,https 连接可以正常工作而不会引发异常。
谢谢!
WCF 客户端的构造函数有一个带有两个参数的重载,binding
和 endpoint
您可以使用此重载来通过所需的安全模式:
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
var xServiceSoapClient = new xServiceSoapClient(binding, "XServiceSoap");
这是一般的想法,使用指定的对象而不是不适用于上述类型操作的服务客户端对象。