Asp.net Static Variable Time to Live through Refresh and PostBack

Decision:

I declared a class-level static variable and initialized the value 0 in ASP.NET 3.5. In the load event, I increment by 1 this variable

Problem:

  • After receiving the page refresh and even Postback , I get the latest values ​​of this variable. Variable declared as STATIC without getting reset on page refresh and Postback ?
  • I just close the browser and close the VS 2008 IDE - although, although I open it again, repeat the same web application, I get the last increment value, Not 0. I am wondering how this is possible after closing the application.

Could you help with this.

+6
static
source share
3 answers

Static variables are valid for the entire AppDomain. When you close the browser, you do not close the application because it continues to run on the web server. Oh, and forgot to mention: try to avoid using static variables in multi-threaded applications without the right locking mechanisms or you may run into race conditions.

+5
source share

Static variables retain their values ​​for the duration of the application domain.

It will survive in many browser sessions until you restart the web server (IIS) or until it restarts on its own (when it decides that it should update its used resources).

+4
source share

Static variables are valid for the entire AppDomain.

Closing the VS 2008 IDE and / or stopping debugging is not always enough to get the AppDomain that hosts your website. (Even if the website is hosted on a Vs 2008 testing server.

One simple solution is to β€œtouch” the web.config file. (For example, add a space and save it)

This will receive the following request processed in the new application domain.

+2
source share

All Articles