Config Transforms are a nifty method to have your configuration vary, depending on which build configuration you are running or what servers you are publishing to. This gets the most useful when you’re deploying to many kinds of different servers; Development, staging, production, etc.
I have a development server, a staging server for acceptance testing and a production server, so I have a different configuration for each setup. For example my development server connects to a development database, development web services, etc., while my production server connects to my production database and a production web services.
This is where Config Transforms come handy. You can have a different configuration for all of your setups. But bear in mind that Config Transforms only work when deploying / publishing your project. So your
Web.config (default config) will always be used if you haven’t deployed / published your project.
1. So let’s begin by going to the
Configuration Manager, where you’ll create a new configuration.
Default we have two configurations;
Release. Debug is used for our development server and Release is used for our production server. Now we need a configuration setup for our staging server. We will use a copy of Release to start off with, but later on we will change it.
2. So go ahead and make a new configuration, copying
Release. Name it
Then you should have something like this:
Now we have a different configuration for staging, but we’re not using it. If you go to the
Solution Explorer and scroll down to Web.config, you’ll see something like:
But our new configuration,
Staging is not there! This is because we have to tell Visual Studio to update our Config Transforms.
3. So now right click on Web.config and click on
Add Config Transform. You’ll see
Web.Staging.config added to the list.
This is great and all, but the configuration is still not doing anything. Let’s change that.
In Web.config, under the
configuration node I have the following:
<appSettings> <add key="WebService" value="//development/service.svc" /> </appSettings>
4. Open up Web.Staging.config and add the following under the
<appSettings xdt:Transform="Replace"> <add key="WebService" value="//staging/service.svc" /> </appSettings>
Now every time you deploy / publish your project with Staging configuration, it will create the
WebService AppSetting with the value of your staging web service URL.
xdt:Transform="Replace". Config Transform has a bunch of ‘commands’ you can use in your config files,
Replace is just one of them, but it is the one I use the most. You can read about all of them over here.
In Web.Release.config I have the following:
<appSettings xdt:Transform="Replace"> <add key="WebService" value="//production/service.svc" /> </appSettings>
So every time I deploy my project to my production server, it will use the production web service URL. Now in my code I can always use
ConfigurationManager.AppSettings["WebService"] (in System.Configuration namespace) and be certain that my project will use the right web service, depending on which configuration I’m using.
Regarding our Connection String, I put a default connection string into Web.config and then add
xdt:Transform="Replace" to the
connectionStrings node in Web.Release.config and Web.Staging.config. Then I add a new key containing my new connection string. Easy as the example above.
Deploying / Publishing
I’m not gonna go into great detail about how to deploy / publish your project. I will maybe write another article for that later on, but I want to show you how you can create a new
Staging publish profile using your
Staging Config Transform we just created.
5. Right click your project and choose
Publish. The following windows will be displayed:
There you can create a new publishing profiles.
6. Click on the drop down list and choose
7. Name your profile something like
StagingProfile and click OK.
I don’t know which publish method you prefer, but I almost always use
File System. It’s easy, it’s fast and it has no config overhead like Web Deploy.
8. After choosing your publish method, click
Next >. There you will choose your Config Transform configuration. This is where we’ll choose
Publish. Now your project will be published using your default Web.config with the replacement of the WebService appSetting or connectionString we put in Web.Staging.config.