WCF ChannelFactory configuration outside of App.config?

I have a windows service that uses a plugin system. I use the following code in the plugin base class to provide a separate configuration for each DLL (so it will read from plugin.dll.config):

string dllPath = Assembly.GetCallingAssembly().Location;
return ConfigurationManager.OpenExeConfiguration(dllPath);

These plugins should make calls for WCF services, so the problem I encountered is that it new ChannelFactory<>("endPointName")only scans the hosted App.config application for endpoint configuration.

Is there a way to just pass ChannelFactory to search in another configuration file or somehow enter my object Configuration?

The only way I can come up with is to manually create an EndPoint and Binding object from the values ​​read from plugin.dll.config, and pass them to one of the overloads ChannelFactory<>. It really looks like a recreation of the wheel, though, and it can get very messy with endPoint, which makes heavy use of behavior and configuration bindings. Perhaps there is a way to easily create EndPoint and Binding objects by passing it a configuration section?

+5
source share
3 answers

There are 2 options.

Option 1. Work with channels.

,.NET 4.0 .NET 4.5 ConfigurationChannelFactory. MSDN :

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();

, , , :

var factory1 = new ConfigurationChannelFactory<ICalculatorChannel>(
        "endpoint1", 
        newConfiguration, 
        null);
ICalculatorChannel client1 = factory1.CreateChannel();

MSDN .

2. .

, ServiceModelSectionGroup. , ConfigurationChannelFactory, ( ChannelFactory IChannelFactory .

Pablo Cibraro : WCF

+2

, ... - , ServiceModelSectionGroup ChannelFactory.

http://weblogs.asp.net/cibrax/archive/2007/10/19/loading-the-wcf-configuration-from-different-files-on-the-client-side.aspx

I use Richard's solution, although it seems to be much cleaner.

0
source

All Articles