Applying Web.config transforms to all config files
Situation
You have a .NET Web app or just a .NET application that has config files. You need this config files to be different for developer’s machine, CI server and Production. Also you have some text/HTML/JS/CSS files that must be adjusted for development, production and CI.
Solution
You’ll be surprized, but even native MSBuild with Web extensions can do it properly. Brief description follows:
1. Create solution configurations
Open your .sln
in Visual Studio and open Configurations manager (click on your current build configuration dropdowh at the top of the window and select Configuration manager).
Create a new configuration(s), one for every needed config files modification. E.g. if you need separate config transformation logic for CI server and Production, create CI
and Production
configurations. You can create empty configurations or clone them from existing ones. I usually clone from Release
config. Checkbox Create project configurations
must be checked.
2. Create config transformations
If you’re not familiar with Web config transforms, go and read the docs! Lots of them over the Internet…
For every config file you have, you should create file(s) named like <FileName>.<Configuration>.<Extension>
, example: for App.config
create App.CI.config
and App.Production.config
. Put corresponding transformations into them.
3. Create replacement files
If you have some plaintext files or other non-XML files to be modified, just create replacement files for all of them like <FileName>.<Configuration>.<Extension>
. Put all needed content to replacement files. And yes, you’ll need to duplicate some parts.
4. Add files to projects
Now we need to add our transforms to project files and tell MSBuild that they are dependent upon our config/original files.
Open your project(s) (.csproj
, .vbproj
, etc.) in text editor. Find your config file record, it looks like this:
You should replace it with this:
You see, we’ve set some additional properties. To be short: we made config file to copy to output folder after build and denied copying for transforms.
In DependentUpon
tag, always use file paths relative to your project directory.
Now you can open your solution in Visual Studio and see how pretty are your config files with folded-in transforms :)
5. Run transforms on config files
Here’s a sample MSBuild XML that does the following:
- Run config transforms on all XML configs. It detects App.config and uses special logic for it.
- Replace needed files.
Place this XML in your project file, preferrably after PropertyGroup definitions in file’s start.
For Web projects, use the following:
It has a side effect: while building, it replaces your Web.config
files with transformed ones (though you can find original content in Web.Base.config
). Originally, transforms run in MSBuild only when you publish the project and this problem doesn’t appear.
blog comments powered by Disqus
Published
Category
msbuildTags
2020
August
- August 11, 2020 » Identifying AWS EBS volumes on instance
June
- June 24, 2020 » AWS S3 Website with private-only access
2018
September
- September 4, 2018 » Multiple ways of PowerShell templating
2017
September
- September 17, 2017 » Link: RDP URI scheme
August
- August 15, 2017 » Link: SSL settings and checks
- August 15, 2017 » Link: Template files substituter for Docker
- August 8, 2017 » Link: Classes in PowerShell
April
- April 15, 2017 » Bash tricks you didn't usually use
2016
August
- August 30, 2016 » Configuring RancherOS for use with AWS autoscaling + Swarm cluster
April
- April 26, 2016 » Building JS assets with MSBuild
- April 25, 2016 » Applying Web.config transforms to all config files
2015
August
- August 29, 2015 » Check if identifier is declared in Bash
2014
April
- April 2, 2014 » PID file management in Bash
- April 2, 2014 » Logging routine for Bash
- April 2, 2014 » Lock file management in Bash