开发者

Static Dictionary<T,T> sharing information accross sessions. How to stop this?

开发者 https://www.devze.com 2023-04-13 02:20 出处:网络
In my web project, I am using Static List. So say suppose I have 2 users (A, B) logged in to my website at the same time, then this List will store some information about A and as well as B. But say w

In my web project, I am using Static List. So say suppose I have 2 users (A, B) logged in to my website at the same time, then this List will store some information about A and as well as B. But say when I process B List's records, A's List's are getting processed instead of B's.

Can somebody point out the problem please?, also please suggest me some possible solution to avoid this problem.

I am using ASP.NET C# 3.5.

Thank you in adv开发者_如何学Pythonance :)

Edit:

Now I have changed the data type from Dictionary to List, but still the same problem...


A static variable is one that is the same for all instances of a particular class. So this means your website uses the exact same dictionary for User A, B, C, D, etc. Essentially whichever user last writes to the dictionary is the one whose content you will see, regardless of which user is looking.

As other's have suggested, you can use Session variables. These are stored in the server's memory, and are related to a specific browser 'session' (i.e. user).

Personally, I prefer to use the ViewState as this is stored in the response to the browser, and returned to you whenever the page makes a postback. This means less memory usage on the server, the viewstate is not volatile across sessions (or subject to application resets like session). However this also means that whatever you are storing is sent across the wire, so you would never want to store things like credit card numbers, SSN's, etc.

It also means that if you're storing a lot of very large objects, you're going to have a very large response and postback (possibly slowing the cycle), so you have to be more careful about how and what you store.

So that's a few different options for you, you should do the research and decide which is best for your requirements.


Storing values in session is like:

Session["mykey"] = value;

And reading values from session is like:

Object value = Session["mykey"];

The session will time out after a couple of minutes and the value would then be null. To avoid this consider using:

Viewstate["mykey"] = value;

Viewstate is used exactly like session except that the value has to be serializable. The viewstate is send to the client and back again so consider the amount of data that you want to store this way. The viewstate is stored in "__VIEWSTATE" and encoded in base64.


Don't use a static dictionary. Use Session variables to store information about a user's session.

For better information, you will have to provide us with a bit more information: what records? in what format? etc.


When a variable is declared static, that means there is exactly 1 instance of that variable per class (this also includes classes in web applications).

Thus, a static variable is not tied to a user. If you need to store data specific to a user, consider using a Session variable.


You could store the dictionary in the Session, that way each user would have their own and the code wouldn't have access to others.

Or if you sometimes want to be able to access other users' info, you could declare it as

static Dictionary<String, Dictionary<T, T>>

where the string key is the unique user id.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号