在使用WCF将客户端连接到服务时,我使用的是通道工厂,如下所示:
new ChannelFactory<T>(endpointConfigurationName);在这种情况下,这将从配置文件加载所有设置。现在我需要在使用通道之前更改URL,这是怎么做的?我没有在ChannelFactory上找到任何网址?我可以在创建通道时提供一个EndpointAddress,但我怀疑这会从configfile中重置我的设置吗?
我使用通道工厂来避免为每个更改生成一个新的代理,并且能够设置凭据。
编辑:
我就是这样解决的
for(int i = 0; i < clientSection.Endpoints.Count; i++)
{
if(clientSection.Endpoints[i].Name == endpointConfigurationName)
{
var endpointAddress = new EndpointAddress(clientSection.Endpoints[i].Address.ToString());
var netHttpBinding = new NetHttpBinding(clientSection.Endpoints[i].BindingConfiguration);
var serviceEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(T)), netHttpBinding, endpointAddress);
var channelFactory = new ChannelFactory<T>(serviceEndpoint);
break;
}
}发布于 2016-11-10 11:23:26
可以使用泛型包装器方法:
public TProxy CreateChannel(string newEndpointAddress)
{
_endpointAddress = newEndpointAddress;
var factory = new ChannelFactory<TProxy>(new NetTcpBinding(), new EndpointAddress(newEndpointAddress));
return factory.CreateChannel();
}发布于 2016-11-10 12:03:59
只是情况是一样的。使用多绑定配置。请参见:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DcServiceBasicBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:801/Service.svc" binding="basicHttpBinding" bindingConfiguration="DcServiceBasicBinding"
contract="Service.IService" name="basicHttpDcServer"/>
<endpoint address="http://10.0.0.1:801/Service.svc" binding="basicHttpBinding" bindingConfiguration="DcServiceBasicBinding"
contract="Service.IService" name="VpnEndpoint"/>
</client>
</system.serviceModel>然后,从您的代码中,这样调用ctor:
public void Init(string endpoint = Config.SERVICE_ENDPOINT) {
_service = new ServiceClient(endpoint);
}其中端点是配置中的端点名称。
P.S.为了个人目的删除了一些名字。
https://stackoverflow.com/questions/40525743
复制相似问题