Objective
This article is part # 3 of Instance Management in WCF.
This article will explain about Singleton Service Instance mode. This article will give theoretical introduction and explanation of coding sample also. It will also discuss about various disadvantages of Singleton Service Instance mode and its potential place of uses.
Part # 1 of this series could be found here.
Part # 2 of this series could be found here.
Singleton Service
-
The Singleton Service Instance created only once.
-
It created when the service host is created.
-
It lives forever.
-
It got disposed only when host shuts down.
-
Singleton service instance does not require client to maintain any logical session.
-
It does not required to use binding that supports transport level session.
Code
Explanation with example
Service Contract 1
Service Contract 2
Service
-
There are two contracts IService1 and IService2.
-
For IService1 session mode is set to SessionMode=SessionMode.Required
-
For IService2 session mode is set to
SessionMode = SessionMode.NotAllowed
-
Service is implanting both the contracts.
-
Service Behavior is configured to single instance mode
ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]
Web.Config
-
In System.ServiceModel, we need to expose two addresses. One each for individual contracts.
Client
Output
Explanation of output
Since service instance is configured for Singleton, all the proxy will share the same service instance. That is why all proxy is incrementing the same value. And as the output, each time client is getting incremented value.
Disadvantage
-
Having scalability problem.
-
All requests to service will run on different worker thread, so it will cause synchronization problem.
-
Only one client could have access to singleton service instance at a time. It will decrease throughput of the service.
-
Responsiveness is very low
-
Singleton Service instance should only be used, when no other options are available. For instance Global Log Book, Single Communication Port etc.
Conclusion
This article explained about, Singleton Service Instance mode. This article gave theoretical introduction and explanation of coding sample also. It also discussed about various disadvantages of Singleton Service Instance mode and its potential place of uses.