views:

18

answers:

1

Suppose I have a .NET project that builds successfully. Now, I need to selectively build to different environments, such as DEV, QA, and PROD. Each require their own config files (app.config for instance) to contain corresponding connection strings and other settings.

I've looked at simple pre-build scripting, such as this one from Scott Hanselman. It works, but my problem is that the pre-build step modifies the source. Simply building the project will overwrite source files and unnecessarily trigger source control modifications.

I could also look into scripting outside of VS/MSBuild to put the config files in there after the deployment, but I'm really hoping there's still a better way. Perhaps a way that's self-contained in VS/MSBuild?

My deployment strategy to the target server is flexible. I've considered a setup project, which would require the configuration take place before the packaging. Or, an xcopy install is ok, too.

Ideas?

A: 

Create a subfolder off of your primary project containing config files with the settings specific to each environment you wish to deploy to. When you build/deploy, these files will be pushed to a subdirectory of the bin folder or the deploy target. Then, define either an MSBuild script, VS post-build behavior or a Commit handler in an install package (maybe all of the above) that either:

  • modifies the app.config to reference the required child config file specifically, or
  • copies one of the child configs to a predefined location and name that the app.config is looking for.

A reference from the main app.config or web.config to the child file might look like:

<connectionStrings configSource="MySeperateConnStringsFile.config" />  
<appSettings configSource="MySeperateAppSettingsFile.config" />

You can do this with custom configuration sections as well; just make sure you set up those sections in the configSections area of the main file like you normally would.

KeithS