问题
Ok, I've got this singleton-like web class which uses session to maintain state. I initially thought I was going to have to manipulate the session variables on each "set" so that the new values were updated in the session. However I tried using it as-is, and somehow, it remembers state.
For example, if run this code on one page:
UserContext.Current.User.FirstName = "Micah";
And run this code in a different browser tab, FirstName is displayed correctly:
Response.Write(UserContext.Current.User.FirstName);
Can someone tell me (prove) how this data is getting persisted 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 a watch, and can see that the session variable is definitely being set somewhere:
(UserContext)System.Web.HttpContext.Current.Session["UserContext"];
As soon as a setter is called the Session var is immediately updated:
set
{
HospitalField = value; //<--- here
ContractField = null;
ModelType = null;
}
回答1:
The UserContext
instance is saved in Session with this line:
System.Web.HttpContext.Current.Session["UserContext"] = uc;
It's not a singleton. The static property UserContext
will attempt to retrieve a instance from Session, and if it doesn't find it, create a new instance and store it in Session.
UPDATE
I can see how the session var is retrieved, my confusion is around how the session var is set.
To add clarification following Micah's comment: the first time the static Current property is accessed, a new UserContext instance is created, its User property is populated with a new User instance, and the UserContext instance is stored in Session. Subsequent accesses to UserContext.Current
(and hence UserContext.Current.User
) in the same session are all accessing the same instance.
If it's still not clear I suggest stepping through with a 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:
Joe is right. Your usage is: UserContext.Current.User.FirstName
In the getter of UserContext.Current
, you're getting back a reference to a piece of memory that lives inside the session object within asp.net. Using any of the setters should/would change that memory and if you inspect the session object either in the debugger or on subsequent lines of code, you should see the same data that you set with your setters.
回答3:
And run this code in a different browser tab, FirstName is displayed correctly:
You are saving it in the session. Opening a new tab can use the same session information as the other tab (I'm not certain about all browsers). Try opening a new browser window (not just a tab), and see what happens.
来源:https://stackoverflow.com/questions/2997901/how-does-this-singleton-like-web-class-persists-session-data-even-though-sessio