There are 2 options.
Option 1. Work with channels.
If you work directly with channels, .NET 4.0 and .NET 4.5 have a ConfigurationChannelFactory . An example MSDN is as follows:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = "Test.config"; Configuration newConfiguration = ConfigurationManager.OpenMappedExeConfiguration( fileMap, ConfigurationUserLevel.None); ConfigurationChannelFactory<ICalculatorChannel> factory1 = new ConfigurationChannelFactory<ICalculatorChannel>( "endpoint1", newConfiguration, new EndpointAddress("http://localhost:8000/servicemodelsamples/service")); ICalculatorChannel client1 = factory1.CreateChannel();
As Langdon noted, you can use the endpoint address from the configuration file by simply passing zero, for example:
var factory1 = new ConfigurationChannelFactory<ICalculatorChannel>( "endpoint1", newConfiguration, null); ICalculatorChannel client1 = factory1.CreateChannel();
This is discussed in the MSDN documentation.
Option 2. Work with a proxy.
If you are working with proxy-generated code, you can read the configuration file and download ServiceModelSectionGroup . There is a bit more work than just using ConfigurationChannelFactory , but at least you can continue to use the generated proxy (which under the hood uses ChannelFactory and manages IChannelFactory for you.
Pablo Cibraro shows a good example of this here: Getting WCF bindings and behavior from any configuration source
Philippe
source share