views:

2429

answers:

8

The way I currently handle this is by having multiple config files such as:

web.config
web.Prod.config
web.QA.config
web.Dev.config

When the project gets deployed to the different environments I just rename the corresponding file with the correct settings.

Anyone have suggestions on how to handle this better?

EDIT: Here are some of the things that change in each config:

  • WCF Client Endpoint urls and security
  • Custom Database configs
  • Session connection strings
  • log4net settings
A: 

It really depends on what the difference is between the environments that is causing you to use different web.config files. Can you give more information as to why each environment currently needs a different one?

Corey Sunwold
I'd imagine each environment would need specialized database connections, state service configuration, and other various configurable items that depend heavily on what environment you're in.
Dan Esparza
A: 

The way we've been doing it is to override the AppSettings section:

<appSettings file="../AppSettingsOverride.config">
    <add key="key" value="override" />    
    ...
</appSettings>

This only works for the appSettings section and so is only useful to a degree. I'd be very interested in more robust solutions.

Edit Below

Just watched this: http://channel9.msdn.com/shows/10-4/10-4-Episode-10-Making-Web-Deployment-Easier/

VS2010 has config transforms which look pretty awesome, should make multiple configurations a complete breeze.

Andrew Barrett
This is interesting -- do you manage each of your AppSettingsOverride.config files in source control too? What's the difference between using your method and using separate web.config files?
Dan Esparza
The biggest advantages are that each developer can have his own override settings. What's in the web.config in source control is what we want on the main deploy environment and we have overrides on our dev and test servers.I'd probably combine this with what's already been said for best effect.
Andrew Barrett
+1  A: 

In Visual Studio, I create xcopy build events and I store all the config files in a /config folder. You only need one event for all configurations if you name your files after the build configuration: i.e. overwriting web.config with /config/web.$(Configuration).config

BC
A: 

We have a few workarounds (not all of them are done with web.config but the same idea)

  1. We include multiple configuration files in the packaged deployment. During installation we specify environment that we are installing on.
  2. Migrate all environment specific settings to the Database server for that environment. WebServer provides its environment when requesting server name
  3. Provide multiple settings (1 per environment) and using code request different settings.
  4. Combination of 2 and 3 (Override a part of the settings based on the environment - for example application server name)
Timur Fanshteyn
+7  A: 

Scott Gu had an article on this once. The solution he presented was to use a Pre-build event to copy the correct config into place depending on the build configuration chosen.

I also noticed that there already is a similar question here on SO.

PHeiberg
A: 

Through most different version management software (subversion, git, etc) you can ignore specific files.

Thus, in subversion, I'd have:

configure.template.php - This file is versioned and contains templated configuration data, such as empty DSN's configure.php - This file is ignored, so that changes to it do not get tracked.

In subversion, the way to do this is:

svn pe svn:ignore . It'll open your editor, then you type configure.php

Save, exit, checkin your changes, and you're good to go.

leftnode
A: 

My favorite way to tackle this is with the configSource attribute. Admittedly I only use this on one element (<connectionStrings>) but it does provide an easy way to swap in and out different segments of a web.config (which I do during install time via a WebSetup project).

Ken Browning
+1  A: 

I also use the web.DEV.config, web.TEST.config, web.PROD.config etc.

I find this way the most easiest, simplest and straight-forward way if your projects are not complex. I don't like making things more complicated than neccessary.

However, I have used NAnt and I think it works well for this. You can set up builds for your different environments. NAnt takes some reading to learn how to use it but it's pretty flexible.

http://aspnet.4guysfromrolla.com/articles/120104-1.aspx

http://nant.sourceforge.net/

I used it along with CruiseControl.net and NUnit to perform automatic daily builds with unit test validation and thought they worked well together.

metanaito