link common external config files in app.config and web.config

Dec 27 2011 Published by raja under .net

a couple of years ago, i worked on a WCF application. the service library had 3 hosts, web + console + windows service. also, the application made extensive use of logging blocks, exception handling blocks, data blocks & validation application blocks. also, there were unit test projects associated with these projects. at one point, what happened was that the app.config & web.config files of the host projects & unit test projects grew like Topsy.

this is because, apart from the data, exception & logging block sections being huge in the config files, the validation section was the largest. primarily because, we were doing configuration based validation using rule sets. (50-70 fields with 3-4 average validations per field. do the math.)

in order to reduce the size of the files & more importantly, remove the tremendous duplication of the sections, i came across this nice post by Tom. after skimming through the initial part, what i found the most suitable, was using a .NET Framework 2.0 feature, to link external config files.

so this is how a linked app.config or web.config file will look like:

<?xml version=”1.0? encoding=”utf-8??>
<configuration>
  <configSections>
    <section name=”validation” type=”blah” />
    <section name=”loggingConfiguration” type=”blah” />
  </configSections>
  <validation configSource=”validations.config” />
  <loggingConfiguration configSource=”logging.config” />
</configuration>

now you can have common files like data.config, validations.config, logging.config etc. which can be shared by host & unit test projects config files. just ensure, they find each other in the same output directory.

No responses yet