dotnet C# Singleton Issues

Singleton's are not a good choice if you are planning for HA (High Availability), but it would work provided HA for the cache is also taken into account and singleton is designed and implemented keeping HA in mind.

Singleton seems to work and behave differently in Classic and Integrated modes because the way requests are handled by IIS 7. If the scenario is coupled with load balancing then definitely Singleton would cease to be a singleton anymore, the impact of which would cause the application to cease functioning as expected sacrificing reliability.

Considering the above technical risks and limitations or problem one way to go about it is. Design the singleton keeping HA in mind plus make the following changes the way the object is constructed. the code for asp-dotnet (asp.net) in C# is as follows. This makes use of the Application object for making an application level singleton.


public class Singleton {
  private static volatile Singleton instance;
  private static object syncRoot = new Object();
 

private Singleton () { 
 Logger.logInfo("new Instance", "Singleton");   
}
 

public static Singleton Instance 
{
 get
  {
   if (instance == null)
   {
    if (Application["Singleton"] != null) {
     instance = (Singleton)

                  Application["Singleton"];
     return instance;
    }
    lock (syncRoot)
     {
      if (instance == null)
       {
        instance = new Singleton();
        Application["Singleton"] = instance;
       }
      }
     }  
    return instance;
   }
 }
}

Comments

Popular posts from this blog

Meaning of Summa Ameen

Halaal Food E Codes

Eid -ul-adha Of Sacrifice or Qurbani 2009