How does this Singleton-like web class store session data, even if the session is not updated in the property settings?

Ok, I have this single-user web class that uses a session to maintain state. Initially, I thought that I would have to manipulate the session variables on each β€œset” so that the new values ​​were updated in the session. However, I tried to use it as is, and somehow it remembers the state.

For example, if you run this code on one page:

UserContext.Current.User.FirstName = "Micah"; 

And by running this code on another browser tab, FirstName is displayed correctly:

 Response.Write(UserContext.Current.User.FirstName); 

Can someone tell me (prove) how this data is saved in the session? Here is the class:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; public class UserContext { private UserContext() { } public static UserContext Current { get { if (System.Web.HttpContext.Current.Session["UserContext"] == null) { UserContext uc = new UserContext(); uc.User = new User(); System.Web.HttpContext.Current.Session["UserContext"] = uc; } return (UserContext)System.Web.HttpContext.Current.Session["UserContext"]; } } private string HospitalField; public string Hospital { get { return HospitalField; } set { HospitalField = value; ContractField = null; ModelType = null; } } private string ContractField; public string Contract { get { return ContractField; } set { ContractField = value; ModelType = string.Empty; } } private string ModelTypeField; public string ModelType { get { return ModelTypeField; } set { ModelTypeField = value; } } private User UserField; public User User { get { return UserField; } set { UserField = value; } } public void DoSomething() { } } public class User { public int UserId { get; set; } public string FirstName { get; set; } } 

I added this to the clock and see that the session variable is definitely set somewhere:

 (UserContext)System.Web.HttpContext.Current.Session["UserContext"]; 

As soon as the setter is called, the var session is immediately updated:

  set { HospitalField = value; //<--- here ContractField = null; ModelType = null; } 
0
source share
3 answers

The UserContext instance is saved in the session with this line:

 System.Web.HttpContext.Current.Session["UserContext"] = uc; 

This is not a singleton. The static property UserContext will try to retrieve the instance from the session, and if it does not find it, create a new instance and save it in the session.

UPDATE

I can see how the var session is retrieved, my confusion about how the var var parameter is set.

To add clarification after Micah’s comment: the first time the static current property is accessed, a new instance of UserContext is created, its User property is populated with a new user instance, and the UserContext instance is stored in the session. Subsequent calls to UserContext.Current (and therefore UserContext.Current.User ) in the same session are available for the same instance.

If still not clear, I suggest going through the debugger.

 public static UserContext Current { get { // If Session does not yet contain a UserContext instance ... if (System.Web.HttpContext.Current.Session["UserContext"] == null) { // ... then create and initialize a new UserContext instance ... UserContext uc = new UserContext(); uc.User = new User(); // ... and store it in Session where it will be available for // subsequent requests during the same session. System.Web.HttpContext.Current.Session["UserContext"] = uc; } // By the time we get here, Session contains a UserContext instance, // so return it. return (UserContext)System.Web.HttpContext.Current.Session["UserContext"]; } } 
+2
source

Joe is right. Your use: UserContext.Current.User.FirstName

In getter UserContext.Current you return a reference to the piece of memory that lives inside the session object in asp.net. Using any of the setters should / should change this memory, and if you check the session object either in the debugger or in the next lines of code, you should see the same data that you set with your setters.

+1
source

And run this code in another browser tab, FirstName is displayed correctly:

You save it in a session. Opening a new tab may use the same session information as another tab (I'm not sure about all browsers). Try opening a new browser window (not just a tab) and see what happens.

0
source

All Articles