ASP.NET 应用程序生命周期 - 如何检查配置属性是否存在?
我编写了一个单例类,它以一种很好的获取属性的方式公开 web.config 属性。
我想要一个 Load 方法来解析配置中的数据并设置公共属性,并且当配置键丢失或无法解析时我想抛出异常(以便它们记录在 EventLog 中)。
我尝试将 Load() 代码放置在 global.asax 的 Application_Start 中,但随后我想起这只会运行一次,或者直到应用程序重新启动为止。
放置用户“每次”启动/运行站点时所需运行的代码的最佳位置在哪里?如果无法加载某些配置属性,我基本上希望网站停止运行。
谢谢。
I've written a singleton class that exposes the web.config properties in a nice get property kind of way.
I want a Load method to parse the data in the config and set the public properties, and I want to throw exceptions (so they are logged in the EventLog) when a configuration key is missing or can't be parsed.
I tried placing the Load() code in Application_Start of the global.asax but then remembered this will only be run once, or until the application restarts.
Where is the best place to put code that you need to run 'everytime' your site is started/run by the user? I basically want the website to stop functioning if certain config properties cannot be loaded.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您更改
web.config
文件时,应用程序池已回收。这意味着下一次点击将导致您的 Application_Start 方法被调用。在此基础上,一旦您的配置发生更改,它将在用户下次访问站点时重新加载,这应该以最少的配置重新加载次数解决问题,而不是每次会话启动时都重新加载例子。因此,您可以这样做(在您的 global.asax 中):
When you change your
web.config
file, the application pool is recycled. This means that the next hit will cause your Application_Start method to be called.On that basis, as soon as your configuration is changed, it will be reloaded the next time a user hits the site, which should resolve the problem with the minimum number of configuration reloads, as opposed to reloading whenever a session starts for example. Therefore, you can do this (in your global.asax):