“寿命” .NET 公共静态变量?
我做了一些实验。在 Login.aspx 按钮的 LoginButton_Click() 事件上,我有一个代码执行以下操作:
MyClass.MyPublicStaticString = LoginNameTextBox.Text;
登录后,它会转到 Default.aspxPage_Load() 上有一个代码,如下所示:
Label1.Text = MyClass.MyPublicStaticString.ToString();
等待几分钟后,即使在我的登录超时到期之前,Label1.Text 也会变空。
这里发生了什么?
I did a little experiment. On the LoginButton_Click() event from a Login.aspx button, I have a code that does something like:
MyClass.MyPublicStaticString = LoginNameTextBox.Text;
After logging in it goes to Default.aspx by FormsAuthentication. On Default.aspx, I have a code on Page_Load() like this:
Label1.Text = MyClass.MyPublicStaticString.ToString();
After waiting for a few minutes, Label1.Text becomes empty even before my login timeout expires.
What is happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
静态字段(除非
[ThreadStatic]
)是每个应用程序域的一个实例,这意味着:所有请求共享相同值。在 Web 应用程序中使用static
时,您需要格外小心。如果有疑问:不要。重新寿命;应用程序域;它们在分配给静态字段时不会被收集,并且如果应用程序池在 IIS 中回收,它们将过期。
Static fields are (unless
[ThreadStatic]
) one instance per app-domain, meaning: all requests share the same value. You need to be exceptionally careful usingstatic
in a web application. If in doubt: don't.Re lifetime; the AppDomain; they won't be collected while assigned to the static field, and will expire if the App-Pool recycles in IIS.
可以使用应用程序状态对象。然而,只要应用程序运行,它所保存的信息就会持续存在。另一方面,会话状态的生命周期与当前用户的访问相关,并且默认为 20 分钟,以验证他/她是否只是暂时吃零食并使用慢速网络。此外,ApplicationState 对所有用户上下文都是可见/可访问的;而Session仅在当前用户的上下文中可见和可访问。第三,回发的上下文仅限于从服务器发送的最后一个页面的上下文,因为当浏览器将页面返回到服务器(包括用户操作的结果)时,就会发生回发。 Page 的生命周期与 Session 和 ApplicationState 的生命周期不同,因此不应将其中任何一个与其他任何一个混淆。
It is possible to use the Application State object. However it holds information that will persist for as long as the application is running. Session State, on the other hand has a lifetime that is tied to the visit by the current user, plus a default of 20 minutes thereafter to verify the s/he is not just snacking temporarily and using a slow network. Besides, ApplicationState is visible / accessible to all user contexts; while Session is visible and accessible only in the context of the current user. On the third hand, Postbacks are limited in context to the context of the last page sent from the Server, because a postback happens when the Browser returns the Page to the Server including the result(s) of the user action(s). The lifecyle of the Page is different from that of the Session, and that of the ApplicationState, and should not be confused, any one with any other.
我从未使用过 ASP.NET,但我可以告诉您静态变量不会过期,或者类似的东西。
我最好的是,这与另一个以某种方式重置变量的请求有关,或者甚至可能重新启动整个应用程序,创建一个全新的内存空间,并且显然没有以前的静态值。
I've never worked with ASP.NET, but I can tell you that static variables do not expire, or anything like that.
My best is that this has to do with another request resetting the variable somehow, or possibly even re-launching the whole application, creating a brand new memory space, and obviously without the previous static value.
您确实应该使用 会话状态对于这种事情。
如果不这样做,短时间内登录的两个用户都会获得最后一个用户的名称,因为它最后存储在
MyPublicStaticString
中。You should really be using Session State for this kind of thing.
If you don't, two users logging in within short period of time will both get last user's name because it was stored last in
MyPublicStaticString
.我认为您的应用程序的 Page 类不再位于您的网络服务器的内存中,这就是您的静态变量消失的原因,但您的身份验证 cookie 仍然有效,因为您的 asp.net 表单身份验证超时可能尚未过期。
I think your application's Page class is not longer in memory at your webserver thats why your static variable disappers but your authentication cookie is still valid as your asp.net forms authentication timeout may not be expired yet.
是否可以使用 ASP.NET 应用程序状态 对象,而不是像 ASP.NET 中那样,这也将在多个回发/会话中维护状态?
Is it possible to use the ASP.NET Application State object instead as in ASP.NET this will also maintain state across multiple Postbacks / Sessions?