Static class to access Session

Shijith MC

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.

Stephen Zeng

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access Session values from static properties of static class

From Dev

Static Template function access static class member

From Dev

TypeScript: access static method of a class

From Dev

Can Not Access Static Class Function?

From Dev

Handling session data using a static class

From Dev

httpcontext with static class to store and retrieve session data

From Dev

Using Powershell to access Static class within a Static Class

From Dev

Access child class static member from a base class static member

From Java

Can I access the static variables of the 'Class' Object?

From Java

Java static constructor access in child class

From Dev

Is using a static class to facilitate access to appsettings acceptable?

From Dev

Access HttpContextBase object from static class

From Dev

Access or get Autofac Container inside a static class

From Dev

Static method cannot access instance members of a class

From Dev

Access static variables within class in Swift

From Dev

iOS - Not able to access Static method of swift class

From Dev

Access to a Java static inner class with Clojure

From Dev

Scala access inherited static member of Java class

From Dev

Access a static variable from a class stored in a variable

From Dev

Access The Type of the Containing Class of a Static Method in Java

From Dev

Access public static variable outside the class

From Dev

Access Form controls from static class

From Dev

Cant access C# static class

From Dev

Understanding non-static class member access

From Dev

better way to access static class fields

From Dev

How to access static methode when its in class

From Dev

How to access the Session in ASP.NET Core via static variable?

From Dev

How to access private static variable in static member function of another class?

From Dev

How to access static property and static constants of another Class Dynamically

Related Related

  1. 1

    Access Session values from static properties of static class

  2. 2

    Static Template function access static class member

  3. 3

    TypeScript: access static method of a class

  4. 4

    Can Not Access Static Class Function?

  5. 5

    Handling session data using a static class

  6. 6

    httpcontext with static class to store and retrieve session data

  7. 7

    Using Powershell to access Static class within a Static Class

  8. 8

    Access child class static member from a base class static member

  9. 9

    Can I access the static variables of the 'Class' Object?

  10. 10

    Java static constructor access in child class

  11. 11

    Is using a static class to facilitate access to appsettings acceptable?

  12. 12

    Access HttpContextBase object from static class

  13. 13

    Access or get Autofac Container inside a static class

  14. 14

    Static method cannot access instance members of a class

  15. 15

    Access static variables within class in Swift

  16. 16

    iOS - Not able to access Static method of swift class

  17. 17

    Access to a Java static inner class with Clojure

  18. 18

    Scala access inherited static member of Java class

  19. 19

    Access a static variable from a class stored in a variable

  20. 20

    Access The Type of the Containing Class of a Static Method in Java

  21. 21

    Access public static variable outside the class

  22. 22

    Access Form controls from static class

  23. 23

    Cant access C# static class

  24. 24

    Understanding non-static class member access

  25. 25

    better way to access static class fields

  26. 26

    How to access static methode when its in class

  27. 27

    How to access the Session in ASP.NET Core via static variable?

  28. 28

    How to access private static variable in static member function of another class?

  29. 29

    How to access static property and static constants of another Class Dynamically

HotTag

Archive