If you need to deploy your app to multiple environments, like in most corporate IT shops (dev, QA, Staging, Training, Production, etc…), then you’ll need to have multiple versions of your config files, or better yet, one config file and “transform” files (one for each deployment environment) that describe ONLY the differences between the main config file and that particular environment.
For example, the connection string in the QA environment is likely different than the connection string in your Dev environment, which is different than your QA environment and again different in your live Production environment.
I’m not going to explain how to WRITE a config transform (how to tell it what needs to change). At least, not in THIS article. But I will tell you how to tell Visual Studio that you have multiple environments and how to make Visual Studio create the basic config transforms for you.
In this example, I’m creating a WCF Service application (works the same with pretty much any web type of application).
- Are you deploying a NON Asp.Net app (like a click-once app or a WinForms or WPF app)? If so, install the Nuget package “Slow Cheetah”. Why? Because Visual Studio has built in support for all this for web.config files, but NOT for app.config files. Slow Cheetah lets you make transforms for ANY file in your project.
- Right-Click your project and choose “Publish…”
- In the “Publish Web” dialog, choose “Custom”
- Give it a name. NOTE! If you have a different admin managing your deployment and/or build servers, you may want to check with them on what name to use, because it will make a difference between whether your stuff works or doesn’t! For this example, I’ll call the transform “QA”
- Choose your deployment method (web publish, file copy, etc…). For this example, I’m choosing “File System” since it requires fewer settings to fill out and I’m going to leave “Target location:” blank. My deployment admin will fill that in later, so I don’t even need to know this. Click “Next”.
- Choose whether this deployment should be a “Release” or a “Debug” deployment. This will cause it to build it as debug or release. (You will also have a debug and a release transform of your web.config file and this new QA transform will inherit from either of those).
- Expand “File Publish Options” and check the items you need, then click “Next”
- Final screen in the wizard. Click “Close”. You can’t click “Publish” if you left the target path blank above.
You’ve now successfully created a publish profile.
Now you’ll need to create a Web.config transform for this profile.
- Rich-Click your QA.pubxml file and choose “Add Config Transform”. Do NOT choose “Add Transform” if you have Slow Cheetah installed.
You now have a new Web.QA.config file.
You can now code your base Web.config file the way you need it to run locally during development. In your Web.QA.config file, you can add transforms to modify settings in your web.config file so that when you build for that environment, Visual Studio will produce a web.config file that’s right for that environment.
You can repeat these steps to add as many publish profiles as you need.
See these images?
You’ll find actual working versions of them at the top and bottom of this article. Please click the appropriate buttons in it to let your friends know about this article.
Check back later for updates too!