Banshee Banshee - 17 days ago 5
C# Question

Change URL on ChannelFactory?

I am using channelfactory when connecting my client to the service using WCF like this :

new ChannelFactory<T>(endpointConfigurationName);


This will load all settings from the config file that is alot in this case. Now I need to change the URL before using the channel, how could this be done? I do not find any URL on the ChannelFactory? I could provide a EndpointAddress while creating the channel but I suspect that this will reset my settings from the configfile?

Im using channelfactory to avoid generating a new proxy for every change and to be able to set credentials.

Edit :

This is how I did solved it

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;
}
}

Answer

You can use a generic wrapper method:

public TProxy CreateChannel(string newEndpointAddress)
{
        _endpointAddress = newEndpointAddress;

        var factory = new ChannelFactory<TProxy>(new NetTcpBinding(), new EndpointAddress(newEndpointAddress));

        return factory.CreateChannel();
}
Comments