.NET 4 web.config 文件重构 - 有什么价值?
根据 .NET 4 中的新增功能:“主要配置元素已被移动到 machine.config 文件,应用程序现在继承这些设置。”
我正在进行一个从 .NET 3.5 升级到 .NET 4 的项目,我对此更改有一些疑问:
- 我认为此更改是可选的:如果我按原样保留当前的 web.config 文件,它应该运行得很好在 .NET 4 下 - 正确吗?
- 此增强功能似乎具有可疑的价值:配置尚未简化 - 复杂性/膨胀刚刚被重新定位到 machine.config 文件而不是 web.config。我错过了什么吗?
- 看来这种增强实际上使部署变得更加困难:除了我们已有的部署步骤之外,现在我们还需要修改 machine.config 文件以确保它包含我们预期的设置/值。
正如你所看到的,我对此的最初看法是:这很麻烦,我不想这样做。我是否遗漏了一些观点,使得这一改变特别有用和有价值?
编辑: Nathan 和 Rob - 你们的回答都非常有帮助并且非常感激 - 很难决定将哪个标记为“真正的”答案。当然,我都投了赞成票。再次感谢!
According to what's new in .NET 4: "major configuration elements have been moved to the machine.config file, and applications now inherit these settings."
I'm on a project upgrading from .NET 3.5 to .NET 4, and I have some questions about this change:
- I assume this change is optional: if I leave my current web.config file as-is, it should run just fine under .NET 4 - correct?
- This enhancement seems to have dubious value: the config hasn't been simplified - the complexity/bloat has just been relocated to the machine.config file instead of web.config. Am I missing something?
- It seems like this enhancement actually makes deployment more difficult: in addition to the deployment steps we already had, now we also need to modify the machine.config file to ensure it contains our expected settings/values.
As you can see, my initial take on this is: it's a hassle and I don't want to do it. Is there some perspective I'm missing that makes this change especially useful and valuable?
EDIT: Nathan and Rob - both your answers were very helpful and greatly appreciated - it was difficult to decide which to flag as the "real" answer. I upvoted both, of course. Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无需修改
machine.config
,除非您希望这些设置成为服务器上运行的所有应用程序的默认设置。从
web.config
中移出的大部分内容都是随 .NET 3 和 3.5 版本添加的“样板”配置项。 ASP.NET 需要它们来注册控件、处理程序等,但您实际上很少需要修改或关心它们。所以基本上这对您很有用,因为对您来说只是“白噪音”的配置项现在已移开,使您可以专注于您真正想要操作的设置。
ScottGu 在他的博客文章中讨论了这个主题:http://weblogs.asp.net/scottgu/archive/2009/08/25/clean-web-config-files-vs-2010-and- net-4-0-series.aspx
You don't ever need to modify the
machine.config
unless you want those settings to be the default for all applications running on the server.Most of what was moved out of the
web.config
were "boilerplate" config items that were added with the release of .NET 3 and 3.5. They were needed by ASP.NET to register controls, handlers, etc, but rarely were they things you would actually need to modify, or care to.So basically this is useful to you because the configuration items that are just "white noise" to you are now moved out of you way, allowing you to focus on the settings you actually want to manipulate.
ScottGu discusses this subject in a blog post of his: http://weblogs.asp.net/scottgu/archive/2009/08/25/clean-web-config-files-vs-2010-and-net-4-0-series.aspx
从我的角度来看,随着“内容”逐渐添加到其中,
web.config
在 .net 1.0 和 .net 3.5 之间不断发展壮大。当我们到达 .net 3.5 时,它充满了我从未使用或修改过的东西。是的,asp.net 运行时需要它,但这不是我的问题!除非您专门更改了某个应用程序已迁移到
machine.config
的设置,否则无需在 web.config 中重新创建它。换句话说,通过改变 .net 1.1 中添加的所有默认值 -> .net 3.5 从 Visual Studio 创建的每一个 web.config 到 machine.config,Microsoft 都使文件变得更干净、更易于阅读。一个经典的例子是这样的:所有这些乱七八糟的东西都可以在 Visual Studio 2008 生成的
web.config
中找到,但它不会出现在 Visual Studio 2010 生成的web.config
中,如下所示:它已被移动到machine.config
它所属的位置(但无法移动到 .net 3.0 / .net 3.5 中,因为它们仍然在 .net 2.0 CLR 上运行)。由于这些很少更改,因此将项目升级到 .net 4.0 并“清理”web.config 文件应该不会导致任何问题。在升级的项目 web.config 文件中保留冗余配置也应该没有什么区别,因为 web.config 中的值只会覆盖 machine.config 中的值。
From my perspective, the
web.config
grew and grew between .net 1.0 and .net 3.5 as "stuff" was incrementally added to it. By the time we hit .net 3.5 it was chock full of things that I never used or modified. Yes, it was needed by the asp.net runtime, but that's not my problem!Unless you've specifically changed a setting that's been migrated to
machine.config
for one of your applications, there's no need to re-create it in your web.config. In other words, by shifting all the defaults that were added in .net 1.1 -> .net 3.5 from every web.config created by Visual Studio, ever tomachine.config
, Microsoft have made the file cleaner and easier to read. A classic example is this:All that mess can be found in a Visual Studio 2008 generated
web.config
, but it's not present in a Visual Studio 2010 generatedweb.config
as it's been moved tomachine.config
where it belongs (but couldn't be moved to in .net 3.0 / .net 3.5 as they still ran on the .net 2.0 CLR).Thanks to the fact that these were seldom changed, upgrading a project to .net 4.0 and "cleaning" the web.config file should cause no issues. Leaving the redundant configuration in the upgraded projects web.config file should also make no difference as the values in the web.config will simply override the values from the machine.config.