Search code examples
c#asp.netenterprise-library

Multiple Configuration Sources for Enterprise Library 4.1?


We use the caching and logging application blocks from entlib 4.1. We want to keep the configuration of those two in seperate files. How can we achieve this?

It looks like entlib is always using the selectedSource as it configuration.

I tried the following:

<?xml version="1.0" encoding="utf-8" ?>    
<configuration> 
  <configSections>    
    <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" />

  </configSections>

  <enterpriseLibrary.ConfigurationSource selectedSource="messagesCache">    
    <sources>    
      <add name="messagesCache" filePath="Configuration\\messagesCache.config"  type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" />    
      <add name="logging" filePath="Configuration\\logging.config"  type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" />    
    </sources>

  </enterpriseLibrary.ConfigurationSource>    
</configuration>

But this doesn't work because the application blocks always use the selectedSource attribute value.


Solution

  • As noted in External configuration files in Enterprise Library for .NET Framework 2.0:

    [...] while you can configure as many Configuration Sources as you want using the tool, only one is ‘selected’ to be the one which Enterprise Library will automatically use [...]

    What I have done is use the configSource attribute:

    <configuration>
      <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <section name="validationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </configSections>
    
      <loggingConfiguration configSource="logging.config"/>
      <exceptionHandlingConfiguration configSource="exceptionHandling.config"/>
      <dataConfiguration configSource="dataAccess.config"/>
      <validationConfiguration configSource="validation.config"/>
    </configuration>
    

    It works great, but the downside is that if you are editing your application/web config file using the configuration tool and save your configuration it will be saved inside the application/web config file.