Tuesday, June 17, 2008

Using External Config Files For .NET Applications

A co-worker of mine came across a blog post about using external configuration files: http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx.

I realized that this can be incredibly useful, as we have had issues with config files at my current job.  Currently, we are managing four different configuration files: one for development, integration, staging and production.  Each of these config files have almost the exact same content, but differ in just a few ways.  I anticipate just having one main config file, but keeping a set of four external config files for those pieces of the configuration files that change.  We'll see how it goes.

I did a little experiment using these external config files on a "Cleanup" console app that I was working on.  I followed the example of the mentioned blog post, but ran into a couple blocks.

One error I got stated that 'The attribute 'configSource' cannot be specified because its name starts with the reserved prefix 'config'.' This happened because I was trying to specify a configSource for the System.ServiceModel section of my app.config.  I was trying to do this because of the fact that the URL's to WCF services in the client section might change.  It turns out that you can only specify a configSource for the child elements of the System.ServiceModel section.  So I changed the section that had a configSource to the 'client' element, and it fixed it.

Once I got that fixed, I ran into another issue stating 'Unable to open configSource file <Config File Name>.', which I had obviously created.  I couldn't figure out why this was happening, but then I realized that the application, due to the fact that it was an executable, was looking in the BIN/Debug folder, not the root of my application.  To remedy this, I made the custom .config files so they would 'Copy Always,' and then my app worked!  You can refer to the screen shot below:

image

8 comments:

smhaig said...

I want to thank you so much for this solution. I have spent HOURS on this and searched all over. This little gem (copy always) in the properties of the config source file is not mentioned anywhere else as far as I have found.

Keep up the good work

Nikos Dimitrakopoulos said...

You just saved me some head-to-wall-hitting :) Thanks a lot!

nepeta said...

thanks, I was already believing the custom configuration had some problem and lost lots of time with it

RSNA Connectathon said...

thanks , you saved my lot of time

CyberChrist - Mokesh - Julian Glenn said...

Excellent little nugget of info (set copy always). I mean I know MS documentation leaves a lot to be desired but you'd think they might want to point this little feature out.

Sharjeel Ahmed said...

Thanks a lot for your useful article. i got stuck on the 'Unable to open configSource file' issue & your article rescued me.
I want to use external config files for storing info APART from appSettings & connectionStrings.. You said in your article that
'It turns out that you can only specify a configSource for the child elements of the System.ServiceModel section.'
Can you tell me how you accomplished that?

skjdk said...

Thank you!!!!You made my day!!

Ravi Gupta said...

this helped, thanks :)