07 May 2010

Singleton Pattern in WCF Service

Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and file systems are prototypical examples. Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system, and therefore require a global point of access. Of course, just when you're certain you will never need more than one instance, it's a good bet you'll change your mind. As a part of performance tuning with our WCF application it is observed that we need to make sure only one instance of service is running and it is not trying to create each instance while making each service call.
The Solution:
The singleton pattern is one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance. A single service instance no matter how many clients call the service might seem like an interesting option since it won’t use up many resources. The drawback however is that you can have multiple calls from multiple clients at the same time. All those calls will be handled by the same service instance. If you only allow one single thread to access the service instance this will drastically reduce the performance. To speed things up you quickly wind up dealing with multiple threads and the obligatory synchronization that ensues.








Singleton Services
The singleton service is the ultimate shareable service. When a service is configured as a singleton, all clients get connected to the same single well-known instance independently of each other, regardless of which endpoint of the service they connect to. The singleton service lives forever, and is only disposed of once the host shuts down. The singleton is created exactly once when the host is created.You can configureasingleton service by setting the InstanceContextMode property to InstanceContextMode.Single:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Multiple)]

Also please see the sample code for implementing

     public class Singleton
     {
       private static Singleton objInstance;
        private Singleton()
        {

        }
        public static Singleton GetInstance()
        {
            if (objInstance == null)
            {
                objInstance = new Singleton();
            }
            return objInstance;
        }
    }
 While every application is unique when it comes to scalability, performance, and throughput, Windows Communication Foundation does offer canonical instance management techniques that are applicable across the range of applications, thus enabling a wide variety of scenarios and programming models. Understanding Windows Communication Foundation instance management and choosing the right activation mode is critical.WE have taken this approach to address the issues in our application.

No comments: