I am trying to implement a static SessionManager class that is supposed to act as a wrapper around a SessionStore object, which is stored in HttpContext.Current.Session["objSession"] and actually holds all of the user's session data. The SessionManager class has identical properties as SessionStore, but has extra methods needed to manipulate the session data as needed. Basically, the SessionManager facilitates getting/setting properties stored in the session object.
All classes are stored in the same namespace as the web solution, and all are serializable.
I have tried two different solutions to my problem, both threw a null reference exception at the same point, when trying to do ANYTHING with HttpContext.Current.Session:
public static class SessionManager
{
static SessionManager()
{
if (HttpContext.Current.Session != null)
{
try
{
if (HttpContext.Current.Session["objStore"] == null)
{
HttpContext.Current.Session["objStore"] = new SessionStore();
}
}
catch (NullReferenceException)
{
HttpContext.Current.Session["objStore"] = new SessionStore();
}
}
}
Code-behind of the calling page:
protected void Page_Load(object sender, EventArgs e)
{
if (SessionManager.groupSettings.Count > 0)
{
pnlDashboard.Visible = true;
pnlLogin.Visible = false;
getDisplayData();
}
else
{
pnlDashboard.Visible = false;
pnlLogin.Visible = true;
}
}
The debugger steps into SessionManager all the way down to line
if (HttpContext.Current.Session != null)
where it then stops and throws the exception. However, when I hover over the code and the properties dialog opens, it shows that the HttpContext.Current.Session object is NOT null. The resulting call stack is here, but indicates that the source line is if (SessionManager.groupSettings.Count > 0), which is in the code-behind:
[NullReferenceException: Object reference not set to an instance of an object.]
Project.Default.Page_Load(Object sender, EventArgs e) in C:\Users\ASP\Project\Project\Default.aspx.cs:20
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952
My second attempt had all of the code within the static constructor above, but had it in a public static sessionStart() method, which was called above the first if statement in the calling page's code-behind:
protected void Page_Load(object sender, EventArgs e)
{
SessionManager.sessionStart()
if (SessionManager.groupSettings.Count > 0)
{
pnlDashboard.Visible = true;
pnlLogin.Visible = false;
getDisplayData();
}
else
{
pnlDashboard.Visible = false;
pnlLogin.Visible = true;
}
}
I am really stumped as to what could be causing this problem. I have static classes elsewhere in my code and haven't had any issues, and the Session seems to not be null.
I appreciate all help. Thank you!
SessionManager.groupSettings? What is that?List<GroupSettings>and actually seems to be the cause of my problems... I initialized all properties withinSessionStoreand it seems that the problem is fixed (perhaps it can't serialize null objects?). I'm not encountering the error anymore and the session seems to be maintaining all values. My next issue is that the debugger now treats "step over" as "Continue." Odd.