1

假设我有一个成功构建的 .NET 项目。现在,我需要选择性地构建到不同的环境,例如 DEV、QA 和 PROD。每个都需要自己的配置文件(例如 app.config)来包含相应的连接字符串和其他设置。

我看过简单的预构建脚本,例如Scott Hanselman 的这个。它有效,但我的问题是预构建步骤修改了源代码。简单地构建项目将覆盖源文件并不必要地触发源代码控制修改。

我还可以研究 VS/MSBuild 之外的脚本,以便在部署后将配置文件放在那里,但我真的希望还有更好的方法。也许是 VS/MSBuild 中自包含的一种方式?

我对目标服务器的部署策略是灵活的。我考虑过一个安装项目,它需要在打包之前进行配置。或者,xcopy 安装也可以。

想法?

4

2 回答 2

2

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!

于 2011-03-22T16:11:49.987 回答
1

从您的主项目中创建一个子文件夹,其中包含配置文件以及您希望部署到的每个环境的特定设置。当您构建/部署时,这些文件将被推送到 bin 文件夹或部署目标的子目录。然后,在安装包(可能是以上所有内容)中定义 MSBuild 脚本、VS 构建后行为或 Commit 处理程序:

  • 修改 app.config 以专门引用所需的子配置文件,或
  • 将其中一个子配置复制到 app.config 正在查找的预定义位置和名称。

从主 app.config 或 web.config 到子文件的引用可能如下所示:

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

您也可以使用自定义配置部分来执行此操作;只需确保像往常一样在主文件的 configSections 区域中设置这些部分。

于 2010-09-21T18:42:47.437 回答