ASP.NET静态变量的生存期 [英] Lifetime of ASP.NET Static Variable

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

问题描述

我捧在页面类中定义(而不是在Global.asax中)静态变量的一些信息。我只在code像声明变量:

I am holding some information in static variables defined in page class (not in Global.asax). I only declare variable in code like:

protected static int SomeGlobalUnsecureID;
protected static string SomeGlobalUnsecureString;

和定义pageLoad的事件的变量。例如,我查身份证从数据库中,如果它是来自SomeGlobalUnsecureID不同,我从别的地方更新SomeGlobalUnsecureID和字符串,否则保持这些设置。这是我的应用程序完全安全的。逻辑(即这些数据并不安全,每个人都可以访问它们,没问题);只是我要完成的就是

and define the variable in PageLoad event. For example, I check ID from the database, if it's different from SomeGlobalUnsecureID, I update SomeGlobalUnsecureID and String from somewhere else, otherwise leave them as is. This is perfectly safe in my app. logic (i.e. those data are not secure, everybody can access them, no problem); only thing I want to accomplish is


  1. 保持相同的内存量,无论连接的用户

  1. Hold the same amount of memory regardless of users connected

更改当且仅当持续的信息是从一个在不同
记忆(因为实际读取的字符串是耗时
我的。

Change if and only if persistent info is different from the one in 'memory' (because actually reading the string is time consuming for me.

现在,因为我使pageLoad的检查,我在重新加载网页没有问题。但是我的页面充满的WebMethods,有时我看到静态变量归零。而奇怪的一部分;会话仍然活跃,即使在静态变量归零(所谓>没有服务器或应用。池重启等)

Now, since I make the check in PageLoad, I have no problems in reloaded pages. However my page is full of WebMethods, and sometimes I see that the static variables are zeroed. And the strange part is; the session is still active even when the static variables are zeroed (so-> no server or app. pool restart etc.)

这是对我来说真的很奇怪。我认为,直到应用程序(在某种程度上)结束静态变量将其持有的价值。但即使是会议没有到期,静态变量归零。你有什么建议?使用应用程序变量更好的选择?我已经在网上阅读所有文件显示静态变量,而不是应用程序变量,是否需要申报他们有些不同?

This is really strange for me. I assume that static variable will hold its value until the application (somehow) ends. But even the Session did not expire, the static variable is zeroed. What do you suggest? Is using application variables a better choice? All documents I've read on web suggest static variables instead of application variables, do I need to declare them somehow different?

推荐答案

静态变量持续的应用程序域的生命。因此,两件事情,会导致你的静态变量重置是一个应用程序域重新启动或使用一类新的。在你的情况与存储在一个aspx页面类的静态变量,你可能会失去静态变量时,ASP.NET决定重新编译aspx页面到一个新的类,用新的替换旧的页面类。

Static variables persist for the life of the app domain. So the two things that will cause your static variables to 'reset' is an app domain restart or the use of a new class. In your case with static variables stored in an aspx Page class, you may be losing the static variables when ASP.NET decides to recompile the aspx Page into a new class, replacing the old page class with the new one.

由于这些原因,如果系统决定重新启动或更换类(。 NET不杀死或卸载在运行中的应用程序域类/组件那么你的静态变量将重置,因为你得到了一类新的重启或更换。这适用于ASPX页面和的classes在APP_ code文件夹

For those reasons if the system decide to restart or replace the class (.NET doesn't kill or unload classes/assemblies in a running app domain then your static variables will reset because you are getting a new class with the restart or replacement. This applies to both aspx Pages and classes in the App_Code folder

如果因任何理由认为需要重新编译它(的请参见ASP.NET动态编译

ASP.NET will replace a class if for any reason thinks that need to recompile it (see ASP.NET dynamic compilation.

您不能prevent从应用程序域重启静态变量的损失,但你可以尽量避免它从类更换。你可以把你的静态变量中的一类,是不是一个aspx页面,是不是在APP_ code目录中。你可能想将它们放置在静态类在某处你的程序。

You can't prevent the loss of static variables from an app domain restart, but you can try to avoid it from class replacement. You could put your static variables in a class that is not an aspx page and is not in the App_Code directory. You might want to place them on a static class somewhere in your program.

public static class GlobalVariables
{
    public static int SomeGlobalUnsecureID;
    public  static string SomeGlobalUnsecureString;
}

静态变量是每个池,也就是意味着,如果你有2个游泳池,运行您的asp.net网站,你有2个不同的静态变量。 ( Web园模式

如果在系统重新启动你的asp.net应用程序用这种方式,静态变量都将丢失。

The static variables are lost if the system restarts your asp.net application with one of this way.


  1. 池决定需要做出重新编译。

  2. 您打开app_offline.htm文件

  3. 您做出池手动重新启动

  4. 池是达到您已经定义并重新启动一些限制。

  5. 为您重新启动IIS的任何理由,或游泳池。

这静态变量不是线程安全的,你需要使用锁定关键字特殊,如果你从不同的线程访问它们。

This static variables are not thread safe, and you need to use the lock keyword especial if you access them from different threads.

由于应用程序重启将重置您的静态无论什么时候,如果你真的想坚持你的数据,你应该使用自定义类中的数据存储在数据库中。您可以存储每个用户的信息,会话状态的用数据库会话状态模式。 ASP.NET应用程序状态/变量不会帮助你,因为它们存储在记忆,而不是数据库,所以他们对应用程序域重新启动输给过。

Since an app restart will reset your statics no matter what, if you really want to persist your data, you should store the data in a database using custom classes. You can store information per-user in Session State with a database session state mode. ASP.NET Application State/Variables will not help you because they are stored in memory, not the database, so they are lost on app domain restart too.

这篇关于ASP.NET静态变量的生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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