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 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"
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.
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!!!