开发者

Asp.net Static Variable Life time Across Refresh and PostBack

开发者 https://www.devze.com 2022-12-11 06:41 出处:网络
WorkAround: I have declared a class level Public static variable and initialized with a value 0 in the environment of ASP.NET 3.5

WorkAround:

I have declared a class level Public static variable and initialized with a value 0 in the environment of ASP.NET 3.5 In load event I Incremented by 1 of that variable

Problem:

  1. After getting page refresh and even Postback, I am getting latest values of that variable. A variable declared as STATIC , not getting reset by Page refresh and Postback?
  2. I just close the browser开发者_如何学运维 and close the VS 2008 IDE - even though while i am reopen, rerun the same web application, I am getting last incremented value, Not 0. I am wondering how this is possible after i close application.

Could you please help on this.


Static variables are valid for the entire AppDomain. When you close your browser you don't close the application as it continues to execute on the web server. Oh and forgot to mention: try to avoid using static variables in multi-threaded applications without proper locking mechanisms or you may run into race conditions.


Static variables keep their values for the duration of the application domain.

It will survive many browser sessions until you restart the web server (IIS) or until it restarts on its own (when it decides it needs to refresh its used resources).


Static variables are valid for the entire AppDomain.

Closing VS 2008 IDE and/or stopping debugging is not always enough to get the AppDomain that is hosting your website to such down. (Even when the website is hosted in the Vs 2008 tests server.

One easy solution is the "touch" the web.config file. (E.g. add a space and save it)

That will get the next request processed in a new app-domain.

0

精彩评论

暂无评论...
验证码 换一张
取 消