Singleton pattern for ASP.NET - Ed's Blog
in

dotnet.org.za

South African .NET Developer Portal

Ed's Blog

Object reference not set to an instance of an object

Singleton pattern for ASP.NET

(I previously posted this on sadeveloper.net, but since I now have my own blog, I thought I'd stick it here as well.)

To implement the singleton pattern (GoF) in ASP.NET you need to put in a bit of a twist if you want to have once instance of an object per web request. The twist I'm talking about has to do with the scope of static variables in ASP.NET. basically a static variable is visible to the whole of a process. Because of the ASP.NET process model, all client requests share the same process and thus the same singleton, this is not always the desired behaviour for example when you want to implement your database connection as a singleton, this will result in all requests sharing the same connection object and even worse, one request will try to use this connection object that was already closed by a previous request.

The solution is to implement the singleton to link the instance of your class to the specific HTTP Context instead of making it a static variable.

The following code is for a singleton class that can be used in both an ASP.NET app and a Winforms app, if a context is not found, a static variable is created as in the default implementation of the pattern.

using System.Web;

public class mySingleton
{

private static mySingleton oInstance;

protected mySingleton()
{
}

public static mySingleton instance()
{
    if (HttpContext.Current == null)
   {
      if (oInstance == null)
      {
         oInstance =
new mySingleton();
      }
      return oInstance;
   }

   if (!HttpContext.Current.Items.Contains("mySingleton"))
   {
      HttpContext.Current.Items.Add("mySingleton",
new mySingleton());
   }

   return (mySingleton)HttpContext.Current.Items["mySingleton"];
}

}

The only thing to remeber here is that the key you use to add your instance to the context object must be unique.

Published Jul 07 2004, 07:39 AM by eduard
Filed under:

Comments

 

arun said:

i want to need defination for singleton object?
February 24, 2005 11:04 AM
 

test said:

testing
March 15, 2005 11:20 AM
 

Steven said:

There are a lot more to a singleton pattern. Just search for polymorphic singleton or multiple instance singleton and see what the different ways of implementing it are.

May 15, 2007 8:57 AM
 

rrossenbg said:

There is just one question.

Is the  instance() method thread save?

February 9, 2008 8:27 AM
 

Mikey said:

Learn to speak english people.

September 3, 2008 9:17 PM
 

To Mickey said:

Fuck you Mickey!!

Is that good enough?

September 9, 2008 12:07 AM
 

To Mikey 2 said:

Yes - Fuck you Mikey...

October 7, 2008 2:40 PM
 

Charles said:

This "modified" singleton is for "per web request".

I want to ask if I want to design for "per web user",

what kind of object should I use to instead "HttpContext.Current"?

Thanks

November 20, 2008 5:27 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Powered by Community Server (Commercial Edition), by Telligent Systems