Is there a way in an asp.net application to return/restore an object from that object's constructor?
For example I have an object called "logging" that I can instantiate in my code with it's "new" constructor like thus:
logging log = new logging();
and somewhere in my code I've saved this object as a session object
httpContext.Current.Session["log"] = log;
Now what I'd like to be able to do is inside the logging constructor, I check for the "saved" session object and if it's there, instead of creating a "new" logging object, I return the one saved in the session object collection (assume session mode is Inproc).
In my logging constructor I tried this but it doesn't fly:
public class logging
{
private string _mainLogLocation = "";
private string _subLogLocation = "";
private level _ReportLevel = level.critical;
public enum level { verbose, inform, warning, error, critical };
public string[] levels = { "verbose", "informational", "warning", "error", "critical" };
public logging()
{
if (HttpContext.Current.Session["log"] != null)
{
this = (logging)HttpContext.Current.Session["log"];
return;
}
// code to create and setup logging properties
}
// method and property definitions
} // end of logging class
The complier does not allow me to make assignemnts to the "this" reference. Any ideas on how to get around this limitation or other ways to accomplish this type of function for this class?
elbilo
Participant
768 Points
178 Posts
how can one restore a cached object from within the objects constructor?
Nov 07, 2012 09:27 PM|LINK
Is there a way in an asp.net application to return/restore an object from that object's constructor?
For example I have an object called "logging" that I can instantiate in my code with it's "new" constructor like thus:
logging log = new logging();
and somewhere in my code I've saved this object as a session object
httpContext.Current.Session["log"] = log;
Now what I'd like to be able to do is inside the logging constructor, I check for the "saved" session object and if it's there, instead of creating a "new" logging object, I return the one saved in the session object collection (assume session mode is Inproc).
In my logging constructor I tried this but it doesn't fly:
public class logging { private string _mainLogLocation = ""; private string _subLogLocation = ""; private level _ReportLevel = level.critical; public enum level { verbose, inform, warning, error, critical }; public string[] levels = { "verbose", "informational", "warning", "error", "critical" }; public logging() { if (HttpContext.Current.Session["log"] != null) { this = (logging)HttpContext.Current.Session["log"]; return; } // code to create and setup logging properties } // method and property definitions } // end of logging classThe complier does not allow me to make assignemnts to the "this" reference. Any ideas on how to get around this limitation or other ways to accomplish this type of function for this class?
niksv
Contributor
5925 Points
1115 Posts
Re: how can one restore a cached object from within the objects constructor?
Nov 18, 2012 11:09 AM|LINK
public class logging { private static logging _logInstance; public static logging GetInstance() { if (HttpContext.Current.Session["log"] != null) _logInstance = (logging)HttpContext.Current.Session["log"]; else _logInstance = new logging(); return _logInstance; } } //Usage in code var log = logging.GetInstance();RameshRajend...
Star
7983 Points
2099 Posts
Re: how can one restore a cached object from within the objects constructor?
Nov 18, 2012 12:19 PM|LINK
Please ref this
http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx