Lifetime of ASP.NET Static Variable

Lifetime of ASP.NET Static Variable

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.

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 will replace a class if for any reason thinks that need to recompile it (see ASP.NET dynamic compilation).

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;
}

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)

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

  1. the pool decide that need to make a recompile.
  2. You open the app_offline.htm file
  3. You make manual restart of the pool
  4. The pool is reach some limits that you have define and make restart.
  5. For any reason you restart the iis, or the pool.

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

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.

lifespan of .NET public static variables?

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.

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.

Lifetime of static variables in .NET

(updated with KeithS's clarification that they aren't read until first used)

They will be read the first time they are used, and then retained until the AppDomain is stopped or recycled, which is probably what you want.

That is, ASP.NET apps run inside an AppDomain. This is how they are resident and available to multiple requests without having to startup for each individual request. You can configure how long they live and when they recycle, etc. Static variables live and die with the app and thus will survive as long as the app is resident in the app domain.

Static variable Behaviour in Asp.Net

So want to know the behavior of the static variable in Asp.Net with
C#.

are static variable equal to global variable which is accessible to
all user? If it is set by user A to true can user B get that value as
True or it has different instance of the variable.

The behavior is like that only if your run your site under one working process on your pool.

If your pool have more than one working process, then each process have their static values, and is unknown to you what process is given to each request, to each user. And process together they are not communicate.

So let say that you have a pool with 4 working process.

UserA ask for a page, Process 1 is replay and set a static value to A.

UserB ask for a page, Process 1 is replay and the static value is A.

UserA ask for a page, Process 2 is replay and the static value is not set.

and so on.
More on the subject:
Lifetime of ASP.NET Static Variable

Where are static variables stored in asp.net aspx page

Using static variables instead of Application state in ASP.NET

Static methods on ASP.NET web sites

Asp.net static object appears sometimes as non global

static variable lifetime and application pool recylcing

Ok, so couldn't help myself, and did a quick test.

This was pretty much as per your example 1, except with a page output so I could do it without being attached to the process,

It confirmed what I thought - The static will be reset to the inline initialize value.

Lifecycle of Static Variables

Static members are associated with the type itself, not with an instance of the type. Therefore their lifecycle is limited to the timing and ordering of their creation, and they don't get "reset" by instances of the type.

ASP.NET C# Static Variables are global?

Yes, in ASP.NET a static fields lifetime is for the app domain (note that this differs a bit for generic types).

I'd recommend using the server Session for storage of data you want to associate with an instance of a client browser session (user login).
i.e.

Session["_groupID"] = Convert.ToInt16(Request.QueryString["GroupID"]);

you can retrieve it by doing:

short groupID = Convert.ToInt16(Session["_groupID"]);


Related Topics



Leave a reply



Submit