Web.config-style transforms for App.config files for C# Console apps?

bwerks picture bwerks · Oct 18, 2013 · Viewed 11.8k times · Source

My team is in love with publish profiles. We have a number of testing environments that we work in, and using Web.config transforms we're able to substitute the names of machines in each environment, configuration settings, etc, which makes our build/deploy/test process much easier.

For example:

Web.config
    Web.dev.config
    Web.int.config
    Web.prod.config

However, we have a number of test applications that we use to hit our web services, implemented as Console applications. We'd like to be able to do the same type of transforms on these config files, so that we can reduce manual labor of testing related to manual editing of config files when picking up a build drop.

Essentially, we want this:

App.config
    App.dev.config
    App.int.config
    App.prod.config

My understanding of these config transforms is that they're related to the corresponding publish profiles in our web projects:

Properties
    PublishProfiles
        dev.pubxml
        int.pubxml
        prod.pubxml

I've attempted to add similar files to our Console app projects, but I suspect it's something in the web publishing MSBuild targets that actually utilizes them.

Is there a way to hook into this part of the build specifically, so that we can transform non-web configs?

Answer

Neelima Reddy picture Neelima Reddy · Feb 6, 2014

To add multiple app.configs to a console application:

I am using visual studio 2013. In this version we don't have a config transformation. Please install the configuration transformation extension.

First add environments in solution configuration. Right click on the App.config file. Click on the Add config Transform. It adds as many as solution configurations.

In App.config:

<appSettings>
    <add key="key" value="some value" />   
</appSettings>

Copy this code in App.Release.Config:

<appSettings>
    <add key="same key which you have given in app.config" value="some new value" xdt:Transform="Replace" xdt:Locator="Match(key)" />   
 </appSettings>

Finally, build the solution.