C# 如何在代码中设置System.ServiceModel.Client.Endpoint对象的address属性?
首先,我不是一名网络开发人员,所以请对我宽容一些 我继承了一个利用web服务客户端的项目。我希望能够跨多个环境部署相同的.config文件,而无需直接编辑它。为此,我不确定如何处理的唯一属性是C# 如何在代码中设置System.ServiceModel.Client.Endpoint对象的address属性?,c#,.net,web-services,wcf,C#,.net,Web Services,Wcf,首先,我不是一名网络开发人员,所以请对我宽容一些 我继承了一个利用web服务客户端的项目。我希望能够跨多个环境部署相同的.config文件,而无需直接编辑它。为此,我不确定如何处理的唯一属性是部分中的地址: <system.serviceModel> <bindings> <basicHttpBinding> <binding name="CA_ServiceSoap" closeTimeout="00:01:00" openTimeout=
部分中的地址:
<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中的
标记中为“address”属性输入一个伪值,然后在运行时通过代码正确定义它?在创建客户端时,可以使用代码中的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));
作为未来的任务,我计划查看是否需要调整这些绑定属性中的任何一个以进行故障排除/性能测试,并将它们添加为appsettings,然后我将在BasicHttpBinding实例化中引用它们