开发者

Recommended way to build a .NET project with selectable configurations

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 script开发者_JS百科ing, 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?


UPDATE 01/09/2012 - I started using Team Build for this project and didn't want to install the Windows SDK on my build machine. I did some research and stumbled across SlowCheetah, which is the way to go for this. There is also a blog post for getting this up and running with Team Build.


Visual Studio XML Transformations on the app.config files. Extending what was put in for the web guys. While I didn't figure any of it out, the following are great resources for getting it set up:

You can get the .targets file with instructions on how to use it from this article. Make sure you read the updates at the end of the article for the most up-to-date info.

You'll also need the information from Vishal Joshi's blog that describes how to set up everything in your project. Simple things like getting the .config files to nest under each other in the solution explorer bring me joy!

If you don't have it already, you'll need to install the .NET Framework 4.0 SDK since the transform stuff uses a tool from there.

Those articles have hints and links to get you going on the transformation syntax. It's pretty straight forward after you see a sample or two.

Good luck!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜