Specifying a dedicated service instance for a singleton service
Normally, when hosting a WCF service, we only need to specify the service type in the ServiceHost
and the runtime will help create the service instance internally, according to the InstanceContextMode
we apply on the service class. This makes hosting a WCF service quite simple and convenient; the service developers do not need to know when and how the runtime creates the service instance. However, in some cases, it is useful and necessary to let the developer control when and how the service instance is created.
Getting ready
Singleton is one of the popular design patterns. This pattern can help design a class that will expose one and only one instance of the class over the entire lifecycle of the class or application context. This is very useful in cases where we want to restrict the number of instances of a given class or component.
How to do it...
In this recipe, we will use a sample Voting
service
to demonstrate how to supply...