This is a mirror of official site: http://jasper-net.blogspot.com/

Loading WCF Client Configuration from Different Files with ConfigurationChannelFactory

| Sunday, September 5, 2010
One pain point with configuration that WCF developers have run into in previous versions of the framework is the limitation that WCF clients can only use the hosting executable’s configuration file. This can be problematic for clients that need to call multiple services because the configuration for all of these services would have to be mashed together in one big configuration file. This can also be a problem for client libraries: since libraries don’t have configuration files, every executable that uses the library needs to provide it with the right configuration in its own configuration file.

Fortunately, this restriction has been lifted in WCF 4.0 with the introduction of ConfigurationChannelFactory. This new class allows you to create clients that can take in any Configuration object to configure your client. This means that you’re no longer limited to using the App.config for your executable. To demonstrate this new class, let’s say you have a client that needs to call out to a web service that returns stock information and another web service that returns weather information.

In WCF 4.0, you can do this:

Configuration stockConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = @"C:\Temp\StockClient.config"}, ConfigurationUserLevel.None);
ConfigurationChannelFactory<StockQuoteSoap> stockChannelFactory = new ConfigurationChannelFactory<StockQuoteSoap>("StockQuoteSoap", stockConfiguration, null);
StockQuoteSoap stockClient = stockChannelFactory.CreateChannel();
GetQuoteResponse stockResponse = stockClient.GetQuote(new GetQuoteRequest { Body = new GetQuoteRequestBody { symbol = "MSFT" } });
Console.WriteLine(stockResponse.Body.GetQuoteResult);
stockChannelFactory.Close();

Configuration weatherConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = @"C:\Temp\WeatherClient.config" }, ConfigurationUserLevel.None);

Read more: Youssef Moussaoui's WCF blog

Posted via email from .NET Info

0 comments: