Sunday, August 8, 2010

Enterprise Library 5.0 – Configuration Services Part - I

Microsoft recently released Ent Lib 5.0. While most of the application blocks are the same as 4.1, one of the new kids on the block is Configuration Services. Hence, I will start off this series on Ent Lib 5.0 by taking a closer look at the Configuration Services.

The configuration services provides three capabilities when it comes to managing config files

  • Ability for multiple applications to use the same config file
  • Ability to use of a specific section from a shared config file
  • Ability for a configuration file to inherit from another config file

In this post, we will see how to enable multiple applications to share the same config file.

Multiple Applications using the Same Config File

I will start off by creating a simple console application. For first timers, right click on the config and you should see a context menu to edit enterprise library configuration.

Clicking on the menu brings up the Ent Lib configuration console. The console has been re-designed in the latest version of enterprise library. To add a configuration source section, there is a menu called “Add Configuration Settings” under Blocks menu. Click on it and you should see a Configuration Section now added. Click on the image icon to expand the section and add a File Based Configuration Source.

Provide the values of the config name and the path of the config file. In the selected source dropdown, select the shared source that we have just configured. Save the settings and right click on the Configuration Sources section and click on Validate. You will get the list of warnings and errors, if any. Let us take a moment to look at the changes in the config. The following section has been added under the <configuration> section

<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" />

<enterpriseLibrary.ConfigurationSource selectedSource="Common Config">
<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" />
<add name="Common Config" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
filePath="CommonConfig" />

When you close the enterprise library console, you should now see a commonconfig.config file in your project path. Right click and include the same in your project. Make sure you change the property of the file to copy over to the output path. This will now be the configuration that your application now uses.

To test this, I have added a logging application block and used the following code to log an entry into the event log.

   1: LogWriter textWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
   2: LogEntry testEntry = new LogEntry();
   3: testEntry.Message = "Logging the loading of EntLibTest";
   4: testEntry.Priority = 2;
   5: testEntry.Categories.Add("General");
   6: textWriter.Write(testEntry);
   8: Console.WriteLine("Message Logged");
   9: Console.ReadLine();

Do remember to make the appropriate configuration changes to ensure logging is enabled. Running the application creates an entry in the event log. I now add another project to the same solution and update the configuration source to point to the commonconfig.config of the first project. Below is a snapshot of how my config for the second project looks like.


You should be able to run the application without making any other changes and ensure that messages are logged from the second console application as well.

Before all you web developers get excited, please remember that this feature does not work with web applications just yet. You can see the details of why in this blog.

That concludes our first look at Configuration Services in Enterprise Library 5.0. I will dig into the other two aspects in subsequent posts. Happy Coding!!!


  1. Hi Vaideeswaran, I tried this and it did not save commonconfig. You started with a config as you clicke on it to get the console up and then you ended with commonconfig in the same project. Does this mean that you have two configs in your project. Also doesn't saving commonconfig in the project defeat the purpose of sharing config. I assumed you would want to save commonconfig in a folder outside the project.

    1. Hi Shawn,

      Yes. There are two configs. The app.config and the commonconfig. The idea of configuration services is to let you use an external shared config apart from the one you have already used. If you are unable to see the commonconfig, can you send me the app.config by email and I will take a look at it?