This will be valid as long as you call it from methods that are involved in
a postback - this is, areas that actually have a context. If you call this
method from areas that don't have a context - such as a timer callback, or a
thread function, or an async method completion (that doesn't go through the
Async Manager), then this won't work.
Now, with that said, your implementation of the Singleton pattern is broken.
For the reasons behind this, see Jon Skeet's Singleton page at (speficially,
your code is almost identical to his canonical broken example):
http://www.yoda.arachsys.com/csharp/singleton.html
I think you should be very leery of using a Singleton in ASP.Net if you're
not aware of concurrency and the issues that can arise from it (which, based
on your singleton code, you're not really aware of yet...).
ASP.Net is a highly concurrent environment, but developers are often
shielded from that. As soon as you introduce a singleton into the mix,
you're going to run into all mannor of threading & concurrency gotchas, and
will end up with some very unexpected results if you're not carefull.
--
Chris Mullins
"RSH" <wa*************@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
If I have a static method that references the HttpContext.Current object
is it safe to be in a static method within a class under ASP .Net?
I know that static classes are shared by all sessions at a given time, so
Im not sure if this can be a static class.
public static SessionManager Instance
{
get
{
HttpContext context = HttpContext.Current;
SessionManager manager = context.Session[SESSION_MANAGER] as
SessionManager;
if (manager == null)
{
manager = new SessionManager();
context.Session[SESSION_MANAGER] = manager;
}
return manager;
}
}
Thanks!
Ron