I have class which has all session variables I use, Can you please let me know whether it's thread safe.
public static class AppSession
{
private const string UserIdKey = "UserId";
public static int UserId
{
get { return GetSession<int>(UserIdKey); }
set { SetSession(UserIdKey, value); }
}
private static T GetSession<T>(string key)
{
var currentSession = HttpContext.Current.Session;
if (currentSession == null) return default(T);
if (currentSession[key] != null)
return (T) currentSession[key];
return default(T);
}
}
While accessing the property AppSession.UserId will it be thread safe.
Yes it is. Each request has its own session, and session is thread safe, becasue:
"the session state module implements a reader/writer locking mechanism and queues the access to state values. A page that has session-state write access will hold a writer lock on the session until the request terminates."
See https://msdn.microsoft.com/en-us/library/aa479041.aspx?f=255&MSPPError=-2147217396
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments