I'm having trouble getting my app to compile. What I'm trying to do is I have a class that has a bunch of cache and session objects. But then I'm putting that whole class within the user's session for easy referencing. This is what I have:
public partial class test_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Globals.Site.SessionTest = "sessionTest2";
Globals.Site.CacheTest = "cacheTest2";
Response.Write("sessionTest = " + Globals.Site.SessionTest + "<br>");
Response.Write("cacheTest = " + Globals.Site.CacheTest);
}
}
public class SiteSettings
{
private string _sessionTest = (string)HttpContext.Current.Session["stest"];
public string SessionTest
{
get
{
return _sessionTest;
}
set
{
HttpContext.Current.Session["stest"] = value;
}
}
private string _cacheTest = (string)HttpContext.Current.Cache["ctest"];
public string CacheTest
{
get
{
return _cacheTest;
}
set
{
HttpContext.Current.Cache["ctest"] = value;
}
}
}
public static class Globals
{
public static SiteSettings Site
{
get
{
if (HttpContext.Current.Session["siteID"] == null)
HttpContext.Current.Session["siteID"] = new SiteSettings();
return (SiteSettings)HttpContext.Current.Session["siteID"];
}
}
}
The compile error I get is:
Unable to cast object of type 'SiteSettings' to type 'SiteSettings'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'SiteSettings' to type 'SiteSettings'.
Source Error:
Line 66: if (HttpContext.Current.Session["siteID"] == null)
Line 67: HttpContext.Current.Session["siteID"] = new SiteSettings();
Line 68: return (SiteSettings)HttpContext.Current.Session["siteID"];
Line 69: }
Line 70: }
Source File: C:\ Line: 68
Am I allowed to put a mixed bag of cache and session items into a session slot of a user?? Not sure if I'm going about this the right way so I would greatly appreciate advice on how to handle this. What my intention is the SiteSetting class will hold some values that are meant to share (like images directory or store email) and other private user data (like cart total). Am I defeating the purpose by putting cache values within a sessioned object class?
public partial class test_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Globals.Site.SessionTest = "sessionTest2";
Globals.Site.CacheTest = "cacheTest2";
Response.Write("sessionTest = " + Globals.Site.SessionTest + "<br>");
Response.Write("cacheTest = " + Globals.Site.CacheTest);
}
}
public class SiteSettings
{
private string _sessionTest = (string)HttpContext.Current.Session["stest"];
public string SessionTest
{
get
{
return _sessionTest;
}
set
{
HttpContext.Current.Session["stest"] = value;
}
}
private string _cacheTest = (string)HttpContext.Current.Cache["ctest"];
public string CacheTest
{
get
{
return _cacheTest;
}
set
{
HttpContext.Current.Cache["ctest"] = value;
}
}
}
public static class Globals
{
public static SiteSettings Site
{
get
{
if (HttpContext.Current.Session["siteID"] == null)
HttpContext.Current.Session["siteID"] = new SiteSettings();
return (SiteSettings)HttpContext.Current.Session["siteID"];
}
}
}
The compile error I get is:
Unable to cast object of type 'SiteSettings' to type 'SiteSettings'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'SiteSettings' to type 'SiteSettings'.
Source Error:
Line 66: if (HttpContext.Current.Session["siteID"] == null)
Line 67: HttpContext.Current.Session["siteID"] = new SiteSettings();
Line 68: return (SiteSettings)HttpContext.Current.Session["siteID"];
Line 69: }
Line 70: }
Source File: C:\ Line: 68
Am I allowed to put a mixed bag of cache and session items into a session slot of a user?? Not sure if I'm going about this the right way so I would greatly appreciate advice on how to handle this. What my intention is the SiteSetting class will hold some values that are meant to share (like images directory or store email) and other private user data (like cart total). Am I defeating the purpose by putting cache values within a sessioned object class?