Thursday, August 19, 2010

Enterprise Library 5.0 – Configuration Services Part -II

Last week, we saw how to use the same configuration file for two different applications. If you have not read the post yet, you can read it here. This week, we will look at how configuration services can help you use a section from a shared configuration file. For the sake of continuity, I will use the same example we used in the previous post.

In the previous post, we added a separate logging section, the change here is that we will just add a redirected section in the Configuration Sources element itself. To enable section redirection, the following has to be done

  • Change the configuration source to System Configuration Source. This will enable the application to use its default app.config
  • Add a redirected section. You can do this by expanding the Configuration Sources section and then add a redirected section. The source of the configuration section should now point to the section in the external file. The screenshot below shows how to add a redirected section


The configuration file now looks like the below

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:     <configSections>
   4:         <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
   5:     </configSections>
   6:     <enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
   7:         <sources>
   8:             <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
   9:             <add name="Shared Config" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  10:                 filePath="C:\Users\r.vaideeswaran\Documents\Visual Studio 2010\Projects\EntLibTest\logging.config" />
  11:         </sources>
  12:         <redirectSections>
  13:             <add sourceName="Shared Config" name="loggingConfiguration" />
  14:         </redirectSections>
  15:     </enterpriseLibrary.ConfigurationSource>
  16: </configuration>

Note how the logging is now configured as a redirectSection. This will ensure that the logging section is picked up from the shared config file. The code in the previous post will now work with this configuration as well without any changes. For the sake of reference, below is the code

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         LogWriter textWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
   6:         LogEntry testEntry = new LogEntry();
   7:         testEntry.Message = "Logging the loading of EntLibTest1";
   8:         testEntry.Priority = 2;
   9:         testEntry.Categories.Add("General");
  10:         textWriter.Write(testEntry);
  12:         Console.WriteLine("Message Logged");
  13:         Console.ReadLine();
  14:     }
  15: }

While in theory, this is the same as using the configSource for a particular setting, one of the advantages is that configSource doesnt permit you to use absolute paths (except for appSettings where you can use the file attribute instead of configSource). Sometimes, this can be a real pain. The Ent Lib Configuration Sources solves that problem.

Hope I was able to give you an idea of how to use a redirected section with Ent Lib 5.0. Happy Coding!!!!


  1. hi,

    Filepath provided here is not logical.

    When many people are working in the project then theis path must be project specific and not local drive specific.

  2. Hi,

    Can we use these configuration services(Enterprise library 5.0 - Configuration Application Block) to read/write custom sections for app/web.config files.

    Vamshi Krishna.

  3. Hi Vamshi,

    You should be able to. This link has more details. Hopefully that helps.