web.config 和用于存储应用程序设置的普通 xml 文件之间的区别

发布于 2024-12-05 16:44:35 字数 188 浏览 0 评论 0原文

我有一个应用程序,它有一些特定的设置,如 erroremailid、maxcapcount 等。 我将这些值存储在应用程序设置块中。谁能告诉我以下两个选项之间哪个更好(性能方面): 1. 在 web.config 中存储设置 2. 将设置存储在普通的 xml 文件中,然后读取它们

一些指定性能差异的文章也很好。

谢谢 阿什瓦尼

I have an application which have some specific settings like erroremailid, maxcapcount etc.
I am storing these values in appsetting block. Can anybody tell me which will be better (performance wise) between following two options:
1. Storing settings in web.config
2. Storing settings in normal xml file and then reading them

Some articles specifying the performance difference will also be good.

Thanks
Ashwani

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

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

发布评论

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

评论(3

够钟 2024-12-12 16:44:35

重点不在于性能,为什么您认为 .NET 框架在读取一个 xml 文件时比读取另一个文件更快?

.NET 的工作方式是从应用程序配置文件中检索某些设置(不仅仅是 appSettings 部分),该文件是 ASP.NET 的 web.configexefile.exe.config 用于可执行应用程序。

在 Windows 窗体应用程序等可执行文件中,您还可以在运行时将应用程序的设置保存在配置文件中,但在 ASP.NET 中,即使 API 相同将允许您调用保存方法,web.config 也不会更新,因为它是以只读方式打开,因为每次更改时 IIS 都会重新启动 Web 应用程序。

无法想象将其他一些自定义设置放在另一个单独的 XML 文件中会更慢或更快,与 web.config 相比的优点是您可以随时编辑此类 xml 文件,而无需重新启动 Web 应用程序每当您在 web.config 中保存更改时就会发生。

仍然。 .NET将从web.config获取appSettings,因此您可以仅将其他类型的设置放入另一个文件中,然后您必须自己手动读取这些设置,不确定是否可以指示ConfigurationManager从另一个文件中读取。

web.config 中还有许多其他部分由应用程序的其他部分获取,如果您将它们移动到某个位置,.NET 不会自动为您获取这些部分,例如默认的 SMTP 服务器。

The point is not about performances, why do you think the .NET framework would be any faster in reading one xml file vs another one?

The way .NET works is that certain settings (not only the appSettings section) are retrieved from the application configuration file which is web.config for ASP.NET and exefile.exe.config for executable applications.

in executables like windows forms applications you can also save settings from the app at runtime in the config file but in ASP.NET even if the APIs being the same will let you call the save method, the web.config will not be updated as it's opened as readonly because every time it is changed IIS restarts the web application.

there is no way to imagine that putting some other custom settings in another separated XML file would be any slower or any faster, the advantage vs the web.config would be that you can edit such xml file anytime without the web app to restart as it would happen anytime you save changes in web.config.

still. .NET will get the appSettings from web.config so you can put in another file only other kind of settings and then you have to read those settings yourself by hand, not sure if you can instruct the ConfigurationManager to read from another file.

There are many other sections inside the web.config that are taken by other parts of the application and if you would move them somewhere .NET would not automatically take those for you, for example the default SMTP Server.

治碍 2024-12-12 16:44:35

您已经有几个类可供您使用,它们允许您从 web.config 中读取设置。如果您选择使用自己的 xml 文件,则必须自己编写代码来读取这些值(尽管这并不是特别困难),

从性能角度来看,web.config 值将被读取一次并存储在内存中(同样,您如果您实现自己的类来从 xml 读取设置,则可以自己执行此操作),因此速度很快。

另一个区别是,asp.net 会自动检测 web.config 文件的更改,并在发生这种情况时再次加载它们。因此,例如,您可以将您的应用程序在运行时指向不同的数据库,而无需回收应用程序。并且应用程序将立即获取更改,而无需您手动回收应用程序。

you have several classes already at your disposal that allow you to read settings from your web.config. if you choose to use your own xml file you will have to write the code to read the values yourself (not that it is particularly difficult, though)

performance wise, the web.config values are read once and strored in memory (again, you could do this yourself if you implement your own class to read settings from your xml) therefore it's fast.

the other difference is that asp.net detects changes to the web.config file automatically and loads them again when this happens. so potentially you can point your app, for example, to a different database at runtime w/o recycling the application. and the application will pick up the changes immediately w/o you recycling the app manually.

执手闯天涯 2024-12-12 16:44:35

webconfig 用于不会更改的设置(不过可以使用内置方法使它们动态更新)。

如果您有很多要更改的设置,那么存储为单独的 XML 将是更好的解决方案,因为您可以即时更新它们。

编辑:

找到一些新信息后,可以动态使用 webconfig 设置以及使用内置方法。 (参见伊卡洛斯的回答)

webconfig for settings that are not going to change (Can use built in methods to make them update on the fly though).

If you have a lot of settings that are being changed, then storing as a seperate XML would be a better solution, since you can update them on the fly.

EDIT:

After finding some new information it is possible to use webconfig settings on the fly as well using built in methods. (See Icarus's answer)

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