.NET配置(app.config / web.config / settings.settings) [英] .NET Configuration (app.config/web.config/settings.settings)
问题描述
我有一个.NET应用程序,它有不同的配置文件的Debug和发行版本。例如。 debug app.config文件指向已启用调试的开发 SQL Server ,并且发布目标指向活SQL Server。还有其他设置,其中一些在调试/版本不同。
I have a .NET application which has different configuration files for Debug and Release builds. E.g. the debug app.config file points to a development SQL Server which has debugging enabled and the release target points to the live SQL Server. There are also other settings, some of which are different in debug/release.
我目前使用两个单独的配置文件(debug.app.config和release.app.config )。我有一个项目的构建事件,说如果这是一个发行版,然后将release.app.config复制到app.config,否则将debug.app.config复制到app.config。
I currently use two separate configuration files (debug.app.config and release.app.config). I have a build event on the project which says if this is a release build then copy release.app.config to app.config, else copy debug.app.config to app.config.
问题是,应用程序似乎从settings.settings文件中获取其设置,所以我必须在Visual Studio中打开settings.settings,然后提示我设置已更改,所以我接受更改,保存settings.settings并且必须重建以使其使用正确的设置。
The problem is that the application seems to get its settings from the settings.settings file, so I have to open settings.settings in Visual Studio which then prompts me that the settings have changed so I accept the changes, save settings.settings and have to rebuild to make it use the correct settings.
是否有更好的/推荐/首选方法实现类似的效果?
Is there a better/recommended/preferred method for achieving a similar effect? Or equally, have I approached this completely wrong and is there a better approach?
推荐答案
任何可能因环境而异的配置都应该存储机器级别,而不是应用程序级别。 (有关配置级别的详细信息。)
Any configuration that might differ across environments should stored at the machine level, not the application level. (More info on configuration levels.)
这些是我通常存储在机器级别的配置元素种类:
These are the kinds of configuration elements that I typically store at the machine level:
- Application settings
- Connection strings
- retail=true
- Smtp settings
- Health monitoring
- Hosting environment
- Machine key
当每个环境(开发人员,集成,测试,阶段,生活)在 c:\Windows \Microsoft.NET\Framework64\v2.0.50727\CONFIG 目录,那么您可以在环境之间升级您的应用程序代码,而不需要任何修改。
When each environment (developer, integration, test, stage, live) has its own unique settings in the c:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG directory, then you can promote your application code between environments without any post-build modifications.
显然,机器的内容-level CONFIG目录在不同的存储库或不同的文件夹结构中从您的应用程序获取版本控制。您可以通过智能使用 configSource 。
And obviously, the contents of the machine-level CONFIG directory get version-controlled in a different repository or a different folder structure from your app. You can make your .config files more source-control friendly through intelligent use of configSource.
我已经做了7年,在超过200个ASP.NET应用程序在25个不同的公司。 (不试图吹嘘,只是想让你知道,我从来没有见过这种方法不会工作的情况。)
I've been doing this for 7 years, on over 200 ASP.NET applications at 25+ different companies. (Not trying to brag, just want to let you know that I've never seen a situation where this approach doesn't work.)
这篇关于.NET配置(app.config / web.config / settings.settings)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!