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

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

问题描述

我在页面类(不是在 Global.asax 中)定义的静态变量中保存了一些信息.我只在代码中声明变量,如:

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 事件中定义变量.例如,我从数据库中检查 ID,如果它与 SomeGlobalUnsecureID 不同,我从其他地方更新 SomeGlobalUnsecureID 和 String,否则保持原样.这在我的应用程序中是完全安全的.逻辑(即那些数据不安全,每个人都可以访问它们,没问题);我唯一想要完成的是

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

当且仅当持久信息与'memory'(因为实际上读取字符串是耗时的我.

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.)

这对我来说真的很奇怪.我假设静态变量将保持其值,直到应用程序(以某种方式)结束.但即使 Session 没有过期,静态变量也被清零.你有什么建议?使用应用程序变量是更好的选择吗?我在网上阅读的所有文档都建议使用静态变量而不是应用程序变量,我是否需要以某种方式声明它们?

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 页面和 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动态编译).

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

您无法防止应用域重启导致静态变量丢失,但您可以尝试避免类替换导致的静态变量丢失.您可以将静态变量放在一个不是 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 个不同的静态变量.(网络花园模式)

The static variables are per pool, that is means that if you have 2 pools that runs your asp.net site, you have 2 different static variables. (Web garden mode)

如果系统使用其中一种方式重新启动您的 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 或池.

这个静态变量不是线程安全的,如果你从不同的线程访问它们,你需要使用lock关键字.

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天全站免登陆