I’ve got a search function that on completion, stores data in a generic list (List<ViewModel>). Up until now, I’ve been assigning the viewmodel value to a static variable to allow me to re-use the data over any page requests that the user may use.
After some reading today though, it seems that the static variable value can be shared across threads, meaning that there is a possibility if I’m viewing the site, that the static variables that contain my search data could be modified by another user.
In the course of my reading I’ve seen solutions such as adding [ThreadStatic] attribute to the variable, although this not only didn’t work, but was roundly dismissed by my further reading as an improper solution.
Others talked about storing variables in HttpContext.Current.Items, but my understanding of that is that it only lasts for a single request.
So, I’m unsure of the best solution here – ideally I ‘d rather not make too many fundamental changes to my application, but in short I’d like to be able to share complex objects across many requests? What is the best method of doing this?
Thanks very much
You can store objects that should be persisted in memory for each user individually in a session (HttpContext.Session) object. Your deployment will of course have to support sessions.