Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Static classes and asp.net sessions

Would someone be kind enough to explain or point to article that explains how the scope of static classes and methods interact with the scope of a ASP.NET user session.

Here is the more specific situation which explains my question:

  1. User A logs into a asp.net website.
  2. While doing something user A uses a static method which initializes some data.
  3. User B logs into the same asp.net website.
  4. User B hits the same static method.

Is the data already initialized once user B hits it?

Also what if the asp.net session of user A expires before user B hits the website?

like image 216
BlueChameleon Avatar asked Sep 28 '12 14:09

BlueChameleon


People also ask

What is ASP.NET session?

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request.

How can use static method in session in C#?

We can not be use the Session object in static method so to its problem while reading the session value in static method ,to overcome this problem we need to read session value like as shown in following code snippet: HttpContext. Current. Session["UserName"].

What is session in ASP.NET MVC?

Session is used to store data values across requests. Whether you store some data values with in the session or not Asp.Net MVC must manage the session state for all the controllers in your application that is time consuming.

What is session in C#?

Session is a feature in ASP.NET Core that enables us to save/store the user data. Session stores the data in the dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at cookie. The SessionId cookie is sent with every request.


2 Answers

Static data lives as long as the process is running. It is attached to the type.

If the data that is initialized is in a static context, then when user B hits the method, it will already be initialized.

Sessions are not relevant here - only the process.

If the process gets recycled, then the data will have to be reinitialized.

like image 109
Oded Avatar answered Sep 28 '22 15:09

Oded


Check these posts: Why does my ASP.Net static function's "context" crossover between user sessions?

What is the scope of a Static Class?

C# Static variables - scope and persistence

http://msdn.microsoft.com/en-us/library/ms173138(v=vs.100).aspx

In short:

Is the data already initialized once user B hits it?

Yes

Also what if the asp.net session of user A expires before user B hits the website?

User B will access data initialized by user A

Static data is shared among the entire application domain of your webapp. It's shared among all the threads serving requests in your webapp, it's not bound to a session/thread/user in any way but to the webapp as a whole.

like image 38
Nathan Avatar answered Sep 28 '22 14:09

Nathan