如何在代码中设置 System.ServiceModel.Client.Endpoint 对象的地址属性?



首先,我不是Web开发人员,所以请放轻松。

我继承了一个利用Web服务客户端的项目。 我希望能够在多个环境中部署相同的 .config 文件,而无需直接对其进行编辑。 为此,我不确定如何处理的唯一属性是<system.serviceModel>部分中的地址:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="CA_ServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1000000" maxBufferPoolSize="524288" maxReceivedMessageSize="1000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="16384" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
<binding name="CA_ServiceSoap1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01: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="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://myserver.contoso.com/CA_Service.asmx" binding="basicHttpBinding" bindingConfiguration="CA_ServiceSoap" contract="WebServices.CA_ServiceSoap" name="CA_ServiceSoap"/>
</client>

有没有办法在 .config 的<endpoint>标签中为"地址"属性输入一个虚拟值,然后在运行时通过代码正确定义它?

创建客户端时,可以在代码中使用basicHttpBinding更改终结点的地址。

根据您提供的配置,我想您创建客户端的代码可能类似于以下内容:

//Specify the binding to be used for the client.
BasicHttpBinding binding = new BasicHttpBinding() { Namespace = "WebServices.CA_ServiceSoap" };
//Specify the address to be used for the client.
EndpointAddress address =
new EndpointAddress("https://myserver.contoso.com/CA_Service.asmx");
// Create a client that is configured with this address and binding.
CA_ServiceSoap client = new CA_ServiceSoap(binding, address);

感谢 @S.Dav 为我指出正确的方向。 我最终所做的是将我发布的 XML 中的所有内容(它本身只是一个序列化对象(直接包含在我的代码中。 唯一可变的属性是 URL:

BasicHttpBinding binding = new BasicHttpBinding()
{
Namespace = "WebServices.CA_ServiceSoap",
CloseTimeout = new TimeSpan(0, 1, 0),
OpenTimeout = new TimeSpan(0,1,0),
ReceiveTimeout = new TimeSpan(0,10,0),
SendTimeout = new TimeSpan(0,1,0),
AllowCookies = false,
BypassProxyOnLocal = false,
HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
MaxBufferSize = 1000000,
MaxBufferPoolSize = 524288,
MaxReceivedMessageSize = 1000000,
MessageEncoding = WSMessageEncoding.Text,
TextEncoding = Encoding.UTF8,
TransferMode = TransferMode.Buffered,
UseDefaultWebProxy = true,
ReaderQuotas = new XmlDictionaryReaderQuotas()
{
MaxDepth = 32,
MaxStringContentLength = 16384,
MaxArrayLength = 16384,
MaxBytesPerRead = 4096,
MaxNameTableCharCount = 16384
}
};
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport = new HttpTransportSecurity()
{
ClientCredentialType = HttpClientCredentialType.None,
ProxyCredentialType = HttpProxyCredentialType.None,
Realm = ""
};
binding.Security.Message = new BasicHttpMessageSecurity()
{
ClientCredentialType = BasicHttpMessageCredentialType.UserName,
AlgorithmSuite = SecurityAlgorithmSuite.Default
};
client = new CA_ServiceSoapClient(binding, new EndpointAddress(Config.WebServiceURL));

作为将来的待办事项,我计划查看是否需要调整这些绑定属性中的任何一个以进行故障排除/性能,并将它们添加为应用程序设置,然后我将在 BasicHttpBinding 实例中引用这些设置。

最新更新