Hi, I am worried about thread-safety in the following code.
If two threads call GetObject() is it possible they both create a new
AlphaContext object and add it to HttpContext.Items?
Can I avoid this with a lock? (I assum I would need a "static" object to
lock on?)
public class ContextFactory
{
private const string OBJECT_KEY = "AlphaSContext";
private ContextFactory()
{
}
public static IContext GetObject()
{
HttpContext httpContext = HttpContext.Current;
IContext context = null;
if (httpContext.Items.Contains(OBJECT_KEY))
{
context = (IContext)httpContext.Items[OBJECT_KEY];
}
else
{
context = new AlphaContext();
httpContext.Items.Add(OBJECT_KEY, context);
}
return (IContext)context;
}
}
Thanks, Peter