views:

262

answers:

1

I have an application dependent on some internal web services, and so we want our development and staging configurations to point to the development and staging servers for the web services.

Right now, this means manually editing my app.config file to point to the appropriate URLs. This is not only a hassle, but prone to human error ("oops, did I not remove that production URL?" can cause many-a-problem).

In a small handful of places in the code, I use the

#if DEBUG
    // do something
#endif

preprocessing statement, and was wondering if something similar could be done for values in the app.config. I've been able to do this just fine with my app Settings, since these values are accessible in-code.

I'm aware of post-build scripts, but it seems like there might be an easier way than writing a routine to munge the app.config XML everytime I do a build. Any suggestions?

This is for C#, and .NET 3.5, and includes both old "web references" as well as the newer WCF "web services" references.

+3  A: 

We used a program called XmlPreprocessor from SourceForge to handle this. It allows you to create parameters in your configuration files and different value files to populate them from.

Given the following files:

app.config

...
<importantSetting>$importantSettingValue$</importantSetting>
...

qavalues.xml

...
<importantSettingValue>QAvalue</importantSettingValue>
...

prodvalues.xml

...
<importantSettingValue>PRODvalue</importantSettingValue>
...

A command line along the lines of following is all that's needed to get the correct values in the correct places.

XmlPreProcess.exe app.config qavalues.xml
Chuck
+1 Great! I was looking for something just like this... It will make my config files a little uglier, but I'd rather have one really ugly one than three moderately ugly ones. (Note: This only applies to config files, not women).
Andrew Rollings