Static variable causing concurrency issue in mvc
We are developing a web application using mvc4. In many cases, we will get a value from the user in the First Page / View, which we need to store in some variable until the user reaches the final page / view. He / she can go through 4-5 views to get the final look at a glance.
To store the value in MVC. We had 3 ways.
1. Global variable . But if I assign a value in one action method. the value will be reset in another action method. So we gave it up.
2.Session . But we need to store more than 5 values in each session. Therefore, we gave up on this.
3.Static Varibale . It works like a charm. but in a few users it caused a concurrency issue.
Are there any other ways to store values in mvc? please guide me.
source to share
Static variables persist for the life of the application domain, which is why you see concurrency issues with multiple users.
See: Static Variables and Their Implications in ASP.Net Websites
There should be no problem storing five values in the session. You can have List<T>
and store this in a session. How:
List<string> someValues = new List<string> {"A","B","C","D", "E",};
HttpContext.Current.Session["userValues"] = someValues;
To get it:
var someValues = HttpContext.Current.Session["userValues"] as List<string>;
if(someValues != null)
{
// found
}
The only thing you should consider is the size of the data. Sessions are stored at a per-user level , storing too much data can cause a problem, although it depends on your configuration.
You can also see: What are ViewData, ViewBag and TempData? - MVC parameters for passing data between current and subsequent requests
source to share