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

[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;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s