"寿命" .NET的公共静态变量? [英] "lifespan" of .NET public static variables?

查看:135
本文介绍了"寿命" .NET的公共静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小的实验的。在 LoginButton_Click()从事件的的Login.aspx 按钮,我有一个code,做一样的东西:

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;

在它之后登录进入 Default.aspx的按FormsAuthentication。在Default.aspx的,我有一个code 的Page_Load()是这样的:

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();

等了几分钟后,Label1.Text变空连的之前我的登录超时。

这是怎么回事?

推荐答案

静态字段(除非 [ThreadStatic] ),每个应用程序域,这意味着一个实例:<强>所有请求共享在相同值。你需要的非常的请小心使用静态在Web应用程序。如有疑问:不要

Static fields are (unless [ThreadStatic]) one instance per app-domain, meaning: all requests share the same value. You need to be exceptionally careful using static in a web application. If in doubt: don't.

重新寿命;在AppDomain;而分配到静态字段,他们将不会被收集,如果应用程序池在IIS中回收过期。

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.

这篇关于&QUOT;寿命&QUOT; .NET的公共静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆