Per Session Static Variables
"The ThreadStatic attribute is what makes this work. The only gotcha so far is that this doesn't work in an ASP.NET application (Web/Webservice) as threads are reused for different requests, hence having the wrong ObjectRegistry serving the wrong user/session. The only solution I can think of for the time being, is having the ObjectRegistry detect if there's a HttpContext, and if so, subscribe to the EndRequest event of the associated HttpApplication and clear itself when the event fires."
[Yves]
Much easier solution for web apps than using ThreadStatic:
public class ObjectRegistry { public static ObjectRegistry Current { get { if(HttpContext.Current.Session["__objectRegistry"] == null)
{
HttpContext.Current.Session["__objectRegistry"] = new ObjectRegistry(); }
return ((ObjectRegistry)HttpContext.Current.Session["__objectRegistry"]); } }
// ... }