The previous two posts on Configuration Services showed how we can use an external configuration file and how we can use a specific section from an external config source.
In the final post of the Configuration Sources series, we will look at what I personally think is one of the most useful features of Configuration Services. We will see how we can have the application configuration file inherit from another configuration file and override or add only sections that are application specific. I will give you a scenario where this can be very useful.
In one of my recent projects we were developing a number of websites. Except for some application specific key value pairs a lot of the other environmental configurations were the same across most of these websites. With this feature, it will be very easy to isolate the environmental specific values in a separate configuration file, inherit the same and add key value pairs to store application specific values.
To show how this works, I have a shared config file that looks like the below.
This is the same configuration as I have used in Part I and II with the logging settings. To use this as my parent configuration, I have to set a couple of properties. I have to set the Parent Source of my config file to the common config file I have created above. The settings are shown in the screenshot below. I have also changed the configuration source to be System Configuration which means my local app.config is being used.
Notice how this configuration file does not have any logging settings. After these changes, my config file now looks like
Notice how there are now two sources added; one is the system configuration source and the other points to the shared configuration file.
As a first step, let me now test the logging code I wrote in the previous two parts. For re-cap, here is the code
This does indeed produce a “Message Logged” output on the console though there are no logging sections directly in my config. This tells me that my parent source is working. The next step is now to add a specific section and see if that works.
I add a new key value pair in the appSettings section through enterprise library. The screenshot for the same is as below.
This is our good old appSettings key value pair. Unsurprisingly, the config is now changed to reflect the above settings.
To make sure my app can retrieve the appSettings value, I add the following three lines of codev(Do not forget the usings at the top)
Nothing fancy. Now when I run the application, you will see that the application picks up both the logging configuration and the appSettings key. This happens because at run time the Enterprise library merges the two configuration files so that they act as one. The above example did not specifically override a section but if I were to override, the value in the local configuration will take precedence over the shared configuration.
That concludes my three part starter posts on Configuration Services. The Configuration Services is no where near perfect yet but as with anything else, I am sure this will evolve over multiple releases. Happy Coding!!!