使用可选配置构建 .NET 项目的推荐方法

发布于 2024-09-24 08:18:39 字数 508 浏览 10 评论 0 原文

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

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

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

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

有想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-10-01 08:18:39

更新 01/09/2012 - 我开始在这个项目中使用 Team Build,并且不想在我的构建计算机上安装Windows SDK。我做了一些研究并偶然发现了 SlowCheetah,它是 实现这一目标的方法。还有一篇博客文章,用于通过 Team Build 启动并运行此功能。


app.config 文件上的 Visual Studio XML 转换。扩展为网络人员提供的内容。虽然我没有弄清楚其中的任何内容,但以下是设置它的重要资源:

您可以从 这篇文章。请务必阅读文章末尾的更新以获取最新信息。

您还需要 Vishal Joshi 的博客 描述了如何设置项目中的所有内容。像在解决方案资源管理器中将 .config 文件嵌套在一起这样简单的事情给我带来了快乐!

如果您还没有,则需要安装 .NET Framework 4.0 SDK,因为转换内容使用其中的工具。

这些文章提供了提示和链接,可帮助您了解转换语法。当你看到一两个样本后,一切就变得非常简单了。

祝你好运!

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!

仙女 2024-10-01 08:18:39

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

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

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文