I have a dll-type project that contains MSTest integration tests. On my machine the tests pass, and I want the same to happen on a CI server (I use TeamCity). But the tests fail, because I need to tweak some settings in app.config. This is why I was thinking to have a separate second app.config file that will hold the settings for CI server.
So I would like to have
/Sln /Proj app.config (I think this is required by VS) app.Release.config (This is a standalone independent config file)
Thus if I select Release configuration in build config on CI, I would like to use app.Release.config file instead of app.config
Problem
This doesn't seem to be straightforward for simple .dll type projects. For web projects, I can do web config transformations. I found a hack how to do these transformations for a dll type project, but I am not a big fan of hacks.
Question
What is a standard approach to tweak app.config files depending on build config for .NET projects (such as Debug, Release, ...)?
You should consider ConfigGen. It was developed for this purpose. It produces a config file for each deployment machine, based on a template file and a settings file. I know that this doesn't answer your question specifically, but it might well answer your problem.
So rather than Debug, Release etc, you might have Test, UAT, Production etc. You can also have different settings for each developer machine, so that you can generate a config specific to your dev machine and change it without affecting any one else's deployment.
An example of usage might be...
If you place this in your .csproj file, and you have the following files...
... then this will be the result...
From the first command, a config file generated for each environment specified in the xls file, placed in the output directory $(SolutionDir)ConfigGen
From the second command, the local App.config used on your dev machine will be replaced with the generated config specified by the local (-l) switch and the filename (-n) switch.
Using the same as approach as Romeo, I adapted it to Visual Studio 2010 :
Here you need to keep both App.config files in different directories (appDebug and appRelease). I tested it and it works fine!
SlowCheetah and FastKoala from the VisualStudio Gallery seem to be very good tools that help out with this problem.
However, if you want to avoid addins or use the principles they implement more extensively throughout your build/integration processes then adding this to your msbuild *proj files is a shorthand fix.
Note: this is more or less a rework of the No. 2 of @oleksii's answer.
This works for .exe and .dll projects:
This works for web projects:
Note that this step happens even before the build proper begins. The transformation of the config file happens in the project folder. So that the transformed web.config is available when you are debugging (a drawback of SlowCheetah).
Do remember that if you create the App_Config folder (or whatever you choose to call it), the various intermediate config files should have a Build Action = None, and Copy to Output Directory = Do not copy.
This combines both options into one block. The appropriate one is executed based on conditions. The TransformXml task is defined first though:
I have solved this topic with the solution I have found here: http://www.blackwasp.co.uk/SwitchConfig.aspx
In short what they state there is: "by adding a post-build event.[...] We need to add the following:
See if the XDT (web.config) transform engine can help you. Currently it's only natively supported for web projects, but technically there is nothing stopping you from using it in other application types. There are many guides on how to use XDT by manually editing the project files, but I found a plugin that works great: https://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859
The plugin is only helping to setup the configuration, it's not needed to build and the solution can be built on other machines or on a build server without the plugin or any other tools being required.
I have heard good things about SlowCheetah, but was unable to get it to work. I did the following: add am tag to each for a specific configuration.
Ex: