Just want to make sure I am not assuming something foolish here, when implementing the singleton pattern in an ASP .Net web application the static variable scope is only for the current user session, right? If a second user is accessing the site it is a different memory scope...?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
- suppress a singleton constructor in java with powe
The static variable scope is for the entire app domain, which means other sessions also have access to it. Only if you have a farm with different servers you would have more than one instance of the variable.
If you need it to be user or session based then check out the following link. Otherwise, as Otavio said, the singleton is available to the entire domain.
http://samcogan.com/singleton-per-asp-net-session/
Static members have a scope of the current worker process only, so it has nothing to do with users, because other requests aren't necessarily handled by the same worker process.
By the way, the default number of worker processes is 1, so this is why the web is full of people thinking that static members have a scope of the entire application.
As others have mentioned, a static variable is global to the entire application, not single requests.
To make a singleton global to only individual requests, you can use the
HttpContext.Current.Items
dictionary.Session for entire application per user. ViewState for single asp page.
The singleton is used for the entire Application Domain, if you want to store user session-related data, use HttpContext Session which is designed for that purpose. Of course, you probably have to redesign your class structure to be able to come up with a key-value-pair way of dealing with the data you're trying to work with.