Sunday, February 10, 2013

WCF : ConfigurationChannelFactory : Loading external configuration files

If you ever wanted to split application configuration (web.config or app.config) from services configuration, .NET Framework 4.0 supports ConfigurationChannelFactory class to read configuration from an external file and can be used to create WCF client proxy dynamically.

I was required to call a WCF service (service) from SharePoint (client) and needed to store WCF serivce client configurations in web.config of the SharePoint web applications. In order to achieve this, here are the steps.

Step1: Use svcutil.exe command line utility to generate a service client object. This object contains only proxy calls to the actual services. The .cs or .vb file generated nees to be included in the project which needs to call the service. In my case, it is a SharePoint project.

Step2: Wherever we need to call the service, here is the code to call the service.
//Load WCF configuration
var servicesConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = @"C:\Test\Test1.config" }, ConfigurationUserLevel.None);
var servicesFactory = new ConfigurationChannelFactory("WSHttpBinding_IService1", servicesConfiguration, new EndpointAddress(System.Web.Configuration.WebConfigurationManager.AppSettings["serviceURL"].ToString()));
_client = servicesFactory.CreateChannel();
_client.nameOfTheMethodToBeCalled();


My configuration is similar to the following in Test1.config directory.



  
    
      
        
          
        
      
    
    
      
    
  

2 comments:

  1. This is very useful. I am writing in C#. I used your above code but am still having a problem when I call the newConfigurationChannelFactory:
    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
    fileMap.ExeConfigFilename = "app.config";
    Configuration newConfiguration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    ConfigurationChannelFactory factory = new ConfigurationChannelFactory("WSHttpBinding_IAlgorithm1",
    newConfiguration, new EndpointAddress("http://localhost:8000/TechnicalAnalysis/Algorithm1"));
    IAlgorithm1 algo = factory.CreateChannel();

    I get the error message when it gets to the factory code:

    ConfigurationChannelFactory factory = new ConfigurationChannelFactory("WSHttpBinding_IAlgorithm1",
    newConfiguration, new EndpointAddress("http://localhost:8000/TechnicalAnalysis/Algorithm1"));

    I get the following error:

    Could not find endpoint element with name 'WSHttpBinding_IAlgorithm1' and contract 'IAlgorithm1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.

    Am wondering whether the client generated code (which I originally generated with a config file using svcutil) needs to be changed.
    Any ideas?
    Thanks!

    ReplyDelete
    Replies
    1. The code I pasted was working code. One difference I found in your code is the path to app.config. Please verify if it works by changing the app.config with full path to the location of the file. Also, it will be helpful if you can paste config file section as well.

      Thanks

      Delete