I am looking at a design pattern which has come up in quite a few of my firm's projects. It has historically functioned correctly, however I have heard some other developers argue that there is a possibility of session corruption using this pattern. I'm looking for insight from other .NET developers here on Stack Overflow.
Basically, there's a class -- usually either static
or a Singleton pattern, depending largely on the developer who wrote it -- stored in App_Code
.
This class encapsulates access to the current session via properties. All of these properties take the form of:
public static class SessionHelper
{
public static string SessionValue
{
get
{
object o = HttpContext.Current.Session["sessionValueName"];
if (o == null)
{
// Replace the following with code to store & retrieve
// a default value of the appropriate datatype.
o = string.Empty;
HttpContext.Current.Session["sessionValueName"] = o;
}
return o.ToString(); // or cast, ensure cast is valid & return.
}
set
{
HttpContext.Current.Session["sessionValueName"] = value;
}
}
// Other properties, strongly-typed, as above.
}
(These are not static when the class is a Singleton.)
I have seen issues with static data on web sites in the past, largely because static data was used to maintain per-session state. (I have, for example, seen statically-declared "per-user" members of code-behinds. Not my code; my team was the clean-up crew for the company that wrote that mess.)
However, because this is just a static entry to HttpContext.Current.Session
, it seems like it should be safe, as it is not fundamentally any different than the Page
class encapsulating this in the Session
property. As I said, no other site on which my company has worked that used this pattern saw it ever have any issues -- and that includes some pretty large and highly active userbases. But I want to just get a fresh perspective.
Are there potential multi-user issues, race conditions, or other failings/flaws which specifically could cause session corruption in the above pattern?