升级时的 web.config 最佳实践
我发送一个应用程序,让客户在 web.config 中更改连接字符串等。
当我升级我的应用程序时,这会引起烦恼,因为我不想用我的值覆盖它们的值。
当 .net 版本升级时,这尤其糟糕。
人们通常如何处理这种情况?
例如,他们是否以某种方式将 web.config 拆分出来,以便客户数据不再是其中的一部分?
I send out an app and I let customers make changes to connection strings and such in the web.config.
When I upgrade my app this causes an annoyance because I don't want to overwrite their values with mine.
This is especially bad when versions of .net are upgraded.
How do people typically handle this type of situation?
For example do they somehow split the web.config out so the customer data is no longer part of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我从未听说过有人让 web.config 可供客户或任何其他业务人员访问和写入。你只是自找麻烦。
听起来您可能想要开发一个小型前端(Web)实用程序,以允许他们以表单形式提交值并将其保存到数据库中。然后让您的应用程序访问数据库来获取这些值,而不是 web.config。
这似乎更多是一个内容管理问题。
I've never heard of anyone making web.config accessible and writable to customers or any other business folk. You're just asking for trouble.
It sounds like you may want to develop a small front-end (web) utility to allow them to submit values in a form and save to a database. Then have your application access the database for these values, and not the web.config.
This seems to be more of a content management issue.
将您的配置文件分成两个。一个为您,另一个为您的客户。
客户可自定义的所有配置都会进入客户配置文件,其他所有内容都会进入您的配置文件。
这将使您可以轻松升级/修改您的配置文件,而无需覆盖客户的配置文件。
您可以使用 SectionInformation.ConfigSource 元素来声明关联的配置文件。 这篇博文 向您展示如何做到这一点。
我什至在此项目中使用它来检测 ASP.NET 中外部配置的更改。
Split your configuration file into two. One for you and the other for your customers.
All configurations that are customizable by your customers go into the customer config file and everything else goes into yours.
This will let you easily upgrade/modify your config file without overwriting your customers'.
You can use the SectionInformation.ConfigSource element to declare associated configuration files. This blog post shows you how you can do it.
I even used it in this project to detect changes to external configurations in ASP.NET.
有几种方法可以处理这个问题。我会提到两个。其中之一涉及您的交付过程。另一个实际上涉及到web.config。
1) 不要将 web.config 作为“代码”发送。将其视为“配置”。这并不适用于所有场景(事实上,基于客户的场景是我想到的糟糕场景)。如果您要交付给“生产”,您可以同意让他们对 web.config 的内容负责(一个好的做法是尝试尽可能多地重构 machine.config)。这样,连接字符串之类的事情就会成为生产问题,而不是开发问题。
2)使用configSource属性。 ASP.NET 2.0 支持使用 configSource 属性来外部化属性。将所有 web.config 作为“生产问题”进行处理可能很困难(在交付给客户的情况下,他们可能不是这方面的专家)。
所以你像这样外部化它。这是您当前的 appSettings 部分,例如:
如果您想要外部化这些设置,以便您的新货件不会覆盖客户设置,请将其替换为:
据我所知,仅在此处相对路径。
参考文献:(
显示该属性是 ASP.NET 2.0 中的新属性)
http://msdn .microsoft.com/en-us/library/system.configuration.sectioninformation.configsource%28v=VS.80%29.aspx
http://www.codeproject.com/KB/aspnet/Manage_Webconfig.aspx
http://trycatchfail.com/blog/post/2008/09/25/Webconfig-magic-configSource-and -文件属性.aspx
There are a few ways to handle this. I'll mention two. One concerns your delivery process. The other actually involves the web.config.
1) Don't ship the web.config as "code". Consider it "configuration". This doesn't apply well to all scenarios (in fact, a customer based scenario is the bad scenario I was thinking of). If you are delivering to "production" you can agree to make them responsible for the contents of web.config (and a good practice there is to try and refactor as much as you can to machine.config). That way, things like the connection string become production concerns and not development concerns.
2) Use the configSource attribute. ASP.NET 2.0 supports externalizing attributes with the configSource attribute. It can be hard to turn over ALL of the web.config as a "production concern" (in a delivery to customer scenario, They may not be experts in all of this).
So you externalize it like this. Here is your current appSettings section, for example:
If these are settings you want to externalize so your new shipments don't override customer settings, replace it with this:
Relative paths only here as far as I know.
References:
(Shows the property is new in ASP.NET 2.0)
http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource%28v=VS.80%29.aspx
http://www.codeproject.com/KB/aspnet/Manage_Webconfig.aspx
http://trycatchfail.com/blog/post/2008/09/25/Webconfig-magic-configSource-and-file-attributes.aspx
你有几个选择。在我看来,最好的办法是当您将应用程序推送到其环境时不要发布 Web 配置。如果需要编写新的配置部分/设置,您可以封装一些逻辑,以编程方式在一个小帮助应用程序中编写新配置,并将其作为部署后操作运行,或者您可以将新设置粘贴到 e - 邮件并发送给您在另一端信任的人以将其放入配置中。在 99% 的情况下我会建议不要使用第二种选择;很可能会出现交叉线或被忽略的情况,那么当系统因配置未成功而出现故障时,这就是您的错。
You have a couple options. The best, IMO, would be to not publish web configs when you push the app to their environment. If a new configuration section/setting needs to be written, you can either encapsulate some logic to programmatically write the new config in a little helper app and run that as a post-deployment action, or you can just paste the new settings into an e-mail and send to someone you trust on the other end to put it in the configs. I would recommend against the second option in 99% of cases; there is a lot of potential for crossing wires or just being ignored, then it's your fault when the system goes down because the configs didn't make it in.