25

I'm a huge fan of the addition of web.config transformations in Visual Studio 2010. See also Scott Hanselman's recent talk at MIX2011.

What sucks is that this functionality (appears at least) to only be available to web projects.

In our solution we have several Windows Services that connect to a different database dependant on the environment they are deployed under.

Has anyone come up with a nice, tidy way of achieving similar 'app.config transformation' functionality?

Note: We are using TFS 2010 to build our solutions in a Continuous Integration manner.

Sayed Ibrahim Hashimi
  • 43,864
  • 17
  • 144
  • 178
isNaN1247
  • 17,793
  • 12
  • 71
  • 118
  • rename to web.config before and rename back again after? Would that work? – Sam Holder May 31 '11 at 11:53
  • 4
    Off topic - But have you seen this tool by the guys at @appharbor http://webconfigtransformationtester.apphb.com/ – superlogical May 31 '11 at 12:12
  • FYI SlowCheetah has preview functionality built into VS http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5 – Sayed Ibrahim Hashimi May 15 '12 at 17:00
  • possible duplicate of [How to select different app.config for several build configurations](http://stackoverflow.com/questions/8082662/how-to-select-different-app-config-for-several-build-configurations) – oleksii Jan 30 '13 at 19:04

5 Answers5

16

I realize you already have an answer, but I stumbled across SlowCheetah this morning which is the best implementation I've seen to date. There is also a blog post on getting this running from a CI server.

John Laffoon
  • 2,885
  • 2
  • 26
  • 38
  • Yup, excellent find. My app.config's are now automatically updated with the appropriate SQL Server connection string, when I deploy, or when I hit F5 to run the app. Excellent find! – Mike Gledhill Jan 09 '13 at 13:33
  • FWIW, active development and support for SlowCheetah has now stopped. The author urges interested parties to vote instead for the feature to be added Visual Studio. See his blog post here: http://sedodream.com/2014/08/11/SlowCheetahIsGoingIntoMaintenanceMode.aspx – Bampfer Dec 19 '14 at 17:10
  • Thanks to number of votes Microsoft received on this, development on SlowCheetah has resumed. The VS 2015 preview can be found here: https://visualstudiogallery.msdn.microsoft.com/05bb50e3-c971-4613-9379-acae2cfe6f9e – John Laffoon Aug 05 '15 at 11:47
15

You can use the XML transformation functionality with any XML file - we do this all the time. It's available via an MSBuild task.

Try adding the following to your build script:

<UsingTask TaskName="TransformXml"
           AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

<TransformXml Source="Path\To\Your\Xml.config"
              Transform="Path\To\Your\Xml.$(Configuration).config"
              Destination="Path\To\Your\Output.config" />
Luke Bennett
  • 32,786
  • 3
  • 30
  • 57
  • 2
    Instead of this SlowCheetah should be used http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5 – Sayed Ibrahim Hashimi Apr 28 '12 at 02:50
  • 5
    That's a very definitive statement - you say it *should* be used without qualifying why, please could you explain further? From what I can see, SlowCheetah is geared around transforming within VS when you press F5. The context here is CI when F5 is no use. SlowCheetah may support a CI scenario, but what else does it offer that makes that approach automatically better than this one? I'm not saying it isn't better, just think it needs justifying :) – Luke Bennett May 15 '12 at 12:20
  • Sure I should have clarified. SlowCheetah is composed of two things: VS UI components to create transforms/preview/etc and MSBuild tasks/targets to perform all the transforms. So the VS components are only needed to add SlowCheetah to a project. To execute these from a CI server/command line is easy. See my blog at http://sedodream.com/2011/12/12/SlowCheetahXMLTransformsFromACIServer.aspx. Hope that clears it up :) – Sayed Ibrahim Hashimi May 15 '12 at 16:49
  • 1
    Thanks for the info, useful to know it supports a CI scenario which I hadn't previously realised (though just noticed now that John Laffoon actually mentioned your blog post in his answer a while back). On a purely CI level (as is the case with this question) does SlowCheetah offer anything additional to Microsoft.Web.Publishing.Tasks.dll? Or is the main advantage in using it just that you get the bonus of VS tooling if you want it? – Luke Bennett May 17 '12 at 09:56
9

I wrote nice extension to automate app.config transformation like the one built in Web Application Project Configuration Transform

Golan Avraham
  • 330
  • 3
  • 5
  • In my case, I see empty files, not the transform I am looking for, for example, my app.cdebug.config is (My assumption was that there will be tag and I can preview the changes) – BeHappy Feb 17 '17 at 21:14
3

Using Luke Bennett's answer to set me off on the right track. I found this to be the answer for us.

FTA (see link for code snippets):

  1. Add a new property ProjectConfigFileName that points to your App.Config file

  2. Add a version of App.Config for each configuration, i.e., App.Debug.config To have them nested under App.Config, edit your csproj file,

  3. Import Microsoft.Web.Publishing.targets into your csproj file right after the Microsoft.CSharp.targets import.

  4. Call the TransformXml task in your AfterBuild target. Note, the BeforeBuild and AfterBuild targets are commented out by default.

Loofer
  • 6,841
  • 9
  • 61
  • 102
isNaN1247
  • 17,793
  • 12
  • 71
  • 118
0

If you have multiple client assemblies and don't want to duplicate the same configuration data, I created Profigurator. It'll take a JSON file as input and apply the settings to an app.config or web.config.

It's a little rough as I write this, but I am currently using it on a production system for deploys and it works great.

Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148