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