global.asax 范围和生命周期澄清
我需要在我当前正在进行的项目中实现多个应用程序级行为。 有几件事我需要弄清楚: 1. 在哪里以及如何定义应用程序级变量? 2. 这些变量的生命周期是多长?或者更准确地说,在什么情况下它们会被丢弃? (应用程序池回收?应用程序二进制文件从内存中删除并在下一个请求时重新编译?等等) 3. global.asax 文件是否是放置会话计数器的好地方,或者将值存储到数据库/文件中可能是保存此类数据的更好方法?
欢迎任何意见或想法。 谢谢你! -埃拉德
I need to implement several application-level behavior in a project I'm currently working on.
There are several things I need to get my head around:
1. Where and how do I define application level variables?
2. What is the lifetime of these variables? or more accuratly, in what scenarios are they discarded? (Application pool recycle? App binaries dropped from memory and recompiled on the next request? etc.)
3. Is the global.asax file a good place to put a session counter, or maybe storing the value to a DB / file is a better way of persisting this kind of data?
Any comments or ideas are welcome.
Thank you!
-Elad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应用程序级变量具有应用程序生命周期。这意味着如果应用程序池被回收,它们就会被丢弃。
应用程序池可以因不同的原因而被回收。可以配置 IIS 6/7,以便在一定时间、一定数量的请求或指定的时间间隔后回收应用程序池。
您可以这样设置应用程序变量:
但是您必须意识到如果您尝试在不同的位置设置/访问可能会遇到的问题。您必须采用一种方法来在更新变量时锁定它们。
我使用
web.config
来设置所有配置参数,然后创建了自己的类,用于存储应用程序字段:如果我需要设置一些字段,我会在应用程序启动时进行设置
应用程序_启动
如果您需要保留信息,您可以创建自己的配置文件(xml 或简单文本)来在应用程序启动和关闭时存储和读取值。您可以在 XML 文件中序列化您的类,以便您可以准备好它,轻松地重新填充您的属性。
一个 db 也可以。
我会对会话计数器做同样的事情。
Application-level variables have an application life-time. It means that it the application pool is recycled, they're discarded.
The application pool can be recycled for different reasons. IIS 6/7 can be configures so that the app pool is recycled after a certain amount of time, after a certain number of request or at specified intervals.
You set an application variable this way:
but you have to be aware of the problems you might encounter if you try to set/access in different place. You have to adopt a way to lock the variables when they're updated.
I us the
web.config
for all the configuration parameters and then I've created my own class which I use to store application fields:If I need set some fields I do it at the application startup
Application_Start
If you need to persist infos you can create your own config file (xml or simple text) to store and read values at the application startup and shutdown. You can serialize your class in a XML file so you can ready it, repopulating your properties easily.
A db would be fine as well.
I would do the same with the session counter.