C# 您如何处理多个环境的多个 web.config 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/592672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you handle multiple web.config files for multiple environments?
提问by Nick
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
- WCF 客户端端点 URL 和安全性
- 自定义数据库配置
- 会话连接字符串
- log4net 设置
采纳答案by PHeiberg
Scott Gu had an articleon 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.
Scott Gu 曾经有一篇关于此的文章。他提出的解决方案是使用预构建事件根据选择的构建配置将正确的配置复制到位。
I also noticed that there already is a similar questionhere on SO.
我还注意到SO 上已经有一个类似的问题。
回答by Corey Sunwold
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?
这实际上取决于导致您使用不同 web.config 文件的环境之间的差异。您能否提供更多信息,说明为什么每个环境目前都需要不同的环境?
回答by Andrew Barrett
The way we've been doing it is to override the AppSettings section:
我们一直在做的方式是覆盖 AppSettings 部分:
<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.
这仅适用于 appSettings 部分,因此仅在一定程度上有用。我对更强大的解决方案非常感兴趣。
Edit Below
在下面编辑
Just watched this: http://channel9.msdn.com/shows/10-4/10-4-Episode-10-Making-Web-Deployment-Easier/
刚看了这个: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.
VS2010 具有看起来非常棒的配置转换,应该使多个配置变得轻而易举。
回答by BC.
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
在 Visual Studio 中,我创建 xcopy 构建事件并将所有配置文件存储在 /config 文件夹中。如果在构建配置之后命名文件,则所有配置只需要一个事件:即用 /config/web.$(Configuration).config 覆盖 web.config
回答by Timur Fanshteyn
We have a few workarounds (not all of them are done with web.config but the same idea)
我们有一些解决方法(并非所有方法都是使用 web.config 完成的,但想法相同)
- We include multiple configuration files in the packaged deployment. During installation we specify environment that we are installing on.
- Migrate all environment specific settings to the Database server for that environment. WebServer provides its environment when requesting server name
- Provide multiple settings (1 per environment) and using code request different settings.
- Combination of 2 and 3 (Override a part of the settings based on the environment - for example application server name)
- 我们在打包部署中包含多个配置文件。在安装过程中,我们指定要安装的环境。
- 将所有环境特定设置迁移到该环境的数据库服务器。WebServer 在请求服务器名称时提供其环境
- 提供多个设置(每个环境 1 个)并使用代码请求不同的设置。
- 2和3的组合(根据环境覆盖部分设置——例如应用服务器名称)
回答by leftnode
Through most different version management software (subversion, git, etc) you can ignore specific files.
通过大多数不同的版本管理软件(subversion、git 等),您可以忽略特定文件。
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.
configure.template.php - 这个文件是版本化的并且包含模板化的配置数据,例如空的 DSN 的 configure.php - 这个文件被忽略,这样对其的更改就不会被跟踪。
In subversion, the way to do this is:
在颠覆中,这样做的方法是:
svn pe svn:ignore . It'll open your editor, then you type configure.php
svn pe svn:ignore 。它会打开你的编辑器,然后你输入 configure.php
Save, exit, checkin your changes, and you're good to go.
保存、退出、签入您的更改,您就可以开始了。
回答by Ken Browning
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).
我最喜欢的解决方法是使用configSource
属性。诚然,我只在一个元素 ( <connectionStrings>
)上使用它,但它确实提供了一种简单的方法来换入和换出 web.config 的不同部分(我在安装时通过 WebSetup 项目执行此操作)。
回答by dtc
I also use the web.DEV.config, web.TEST.config, web.PROD.config etc.
我也使用 web.DEV.config、web.TEST.config、web.PROD.config 等。
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.
但是,我使用过 NAnt,我认为它适用于此。您可以为不同的环境设置构建。NAnt 需要一些阅读来学习如何使用它,但它非常灵活。
http://aspnet.4guysfromrolla.com/articles/120104-1.aspx
http://aspnet.4guysfromrolla.com/articles/120104-1.aspx
I used it along with CruiseControl.net and NUnit to perform automatic daily builds with unit test validation and thought they worked well together.
我将它与 CruiseControl.net 和 NUnit 一起使用,通过单元测试验证执行自动日常构建,并认为它们可以很好地协同工作。
回答by user1824408
Transforms seem really helpful for this. You can replace certain sections with different rules.
转换似乎对此很有帮助。您可以用不同的规则替换某些部分。
http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx