Is it safe to store and update list in asp.net session via static methods?

beginner

Is that code below thread safe? If not could you please suggest the correct approach how to make it thread safe? I just want to avoid Lock if it's redundant

public static IList<string> Urls => urlList;

public static bool AddUrl(string url)
{
  var list = urlList;
  if (list.Contains(url))
  {
    return true;
  }

  list.Add(url);
  urlList = list;
  return false;
}

private static IList<string> urlList
{
  get
  {
    List<string> list = null;
    var sessionValue = HttpContext.Current.Session[sessionKey];
    if (sessionValue != null)
    {
      list = sessionValue as List<string>;
    }

    if (list == null)
    {
      list = new List<string>();
    }

    return list;
  }
  set
  {
    HttpContext.Current.Session[sessionKey] = value;
  }
}
Patrick Hofman

Thread safety is the least of your concerns. static variables are shared across sessions. You are probably sharing information that should reside in the user context with other users (since you store it in the session rather than the application cache).

For thread safety I would indeed introduce lock. There is another thing that might cause problems when starting a new thread by hand: the HttpContext.Current.Session is not available in that thread since it is specific for the thread the request runs in.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Why are static methods in .Net framework classes always thread-safe?

From Dev

Static HttpClient thread safe on ASP.net HttpRequest

From Dev

List String Session Variables in ASP.NET

From Dev

Session State SQLServer deleting static variables in asp.net

From Dev

Store password like a plain text in Session ASP .NET MVC

From Dev

Calling static async methods from ASP.net project

From Dev

Static Methods for Data Access Layer in ASP.Net

From Dev

How to update asp net webforms or mvc application without losing session?

From Dev

Static methods vs non static methods in .Net

From Dev

Is it safe to store a logged_in option as a SESSION?

From Dev

Is Session variable thread-safe within a Parallel.For loop in ASP.Net page

From Dev

Is Session variable thread-safe within a Parallel.For loop in ASP.Net page

From Dev

How to store data in a static class to database in ASP. NET? And modify the value for the static class?

From Dev

How to store a list in client side in asp.net

From Dev

Store session variable in action filter or static method

From Dev

httpcontext with static class to store and retrieve session data

From Dev

Static property in ASP.Net based on session will be shared across all users?

From Dev

ASP.NET 5 (Core): How to store objects in session-cache (ISession)?

From Dev

Storing user data in session store or retrieving from database in each request in asp.net core?

From Dev

how to store Gridview Boolean value in session variable and show another page in asp.net

From Dev

Laravel Session access through Middleware Methods - Session store not set on request

From Dev

Rails + Sessions: Safe to store partial credit card info in session?

From Dev

ASP.Net 5 (MVC 6) - Accessing session object in view via a wrapper

From Dev

ASP.Net 5 (MVC 6) - Accessing session object in view via a wrapper

From Dev

Is it safe to use static methods on File class in C#?

From Dev

Is it safe to use static methods in J2EE WebFilter filters?

From Dev

Does XmlHTTPRequest/AJAX request on Asp.net MVC controller update session timeout

From Dev

ASP.Net MVC and state - How to update the database from Session changes in dual listboxes?

Related Related

  1. 1

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

  2. 2

    Why are static methods in .Net framework classes always thread-safe?

  3. 3

    Static HttpClient thread safe on ASP.net HttpRequest

  4. 4

    List String Session Variables in ASP.NET

  5. 5

    Session State SQLServer deleting static variables in asp.net

  6. 6

    Store password like a plain text in Session ASP .NET MVC

  7. 7

    Calling static async methods from ASP.net project

  8. 8

    Static Methods for Data Access Layer in ASP.Net

  9. 9

    How to update asp net webforms or mvc application without losing session?

  10. 10

    Static methods vs non static methods in .Net

  11. 11

    Is it safe to store a logged_in option as a SESSION?

  12. 12

    Is Session variable thread-safe within a Parallel.For loop in ASP.Net page

  13. 13

    Is Session variable thread-safe within a Parallel.For loop in ASP.Net page

  14. 14

    How to store data in a static class to database in ASP. NET? And modify the value for the static class?

  15. 15

    How to store a list in client side in asp.net

  16. 16

    Store session variable in action filter or static method

  17. 17

    httpcontext with static class to store and retrieve session data

  18. 18

    Static property in ASP.Net based on session will be shared across all users?

  19. 19

    ASP.NET 5 (Core): How to store objects in session-cache (ISession)?

  20. 20

    Storing user data in session store or retrieving from database in each request in asp.net core?

  21. 21

    how to store Gridview Boolean value in session variable and show another page in asp.net

  22. 22

    Laravel Session access through Middleware Methods - Session store not set on request

  23. 23

    Rails + Sessions: Safe to store partial credit card info in session?

  24. 24

    ASP.Net 5 (MVC 6) - Accessing session object in view via a wrapper

  25. 25

    ASP.Net 5 (MVC 6) - Accessing session object in view via a wrapper

  26. 26

    Is it safe to use static methods on File class in C#?

  27. 27

    Is it safe to use static methods in J2EE WebFilter filters?

  28. 28

    Does XmlHTTPRequest/AJAX request on Asp.net MVC controller update session timeout

  29. 29

    ASP.Net MVC and state - How to update the database from Session changes in dual listboxes?

HotTag

Archive