40

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...?

Majid
  • 13,853
  • 15
  • 77
  • 113
Wesly
  • 679
  • 2
  • 7
  • 11

6 Answers6

40

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.

  • In order to share data with a specific user and across requests, use HttpContext.Current.Session.
  • In order to share data within a specific request, use HttpContext.Current.Items.
  • In order to share data across the entire application, either write a mechanism for that, or configure IIS to work with a single process and write a singleton / use Application.

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.

Moshe Bixenshpaner
  • 1,840
  • 1
  • 17
  • 23
38

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.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
  • 4
    +1 "Only if you have a farm with different servers you would have more than one instance of the variable". – Jim Aho Jul 26 '16 at 12:20
17

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.

public class Singleton
{
    private Singleton() { }

    public static Singleton Instance 
    {   
        get
        {
            if (HttpContext.Current.Items["yourKey"] == null)
                HttpContext.Current.Items["yourKey"] = new Singleton();
            return (Singleton)HttpContext.Current.Items["yourKey"];
        }
    }
}
Community
  • 1
  • 1
Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
  • Thanks, What is the overhead of this? will it be much slower if I am accessing the singleton quite often? – Wesly Jan 25 '10 at 18:40
  • @Ws You shouldn't notice any performance issues with this approach. The dictionary implementation is pretty efficient so it won't slow your app down even if you access it a lot. – Dan Herbert Jan 25 '10 at 19:16
  • why should we use "HttpContext.Current.Items" instead of "Session"? – alansiqueira27 Oct 09 '14 at 13:09
  • @Seva The lifetime of HttpContext.Current.Items is the current request. ie. 1 request. HttpContext.Current.Session spans the entire session. ie. multiple requests that are part of the same session. – Haohmaru Nov 13 '14 at 14:52
  • That is very nice and exactly what I am looking for. Is there some type save aproach availbe 5 years later? – Christian Gollhardt Sep 17 '15 at 18:03
4

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/

Sam Cogan
  • 4,124
  • 7
  • 44
  • 76
NotMe
  • 87,343
  • 27
  • 171
  • 245
  • 2
    @Michael Lang: Yes it was, looks like sam changed his content engine. Updated the link with the new reference. – NotMe Jul 14 '11 at 16:51
0

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.

Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
-3

Session for entire application per user. ViewState for single asp page.

Johnny W
  • 1
  • 2