Instance Management in WCF Part # 3

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

InsMgmt1.gif

InsMgmt2.gif
 

  1. The Singleton Service Instance created only once.
  2. It created when the service host is created.
  3. It lives forever.
  4. It got disposed only when host shuts down.
  5. Singleton service instance does not require client to maintain any logical session.
  6. It does not required to use binding that supports transport level session.

    InsMgmt3.gif

Code

InsMgmt4.gif

Explanation with example

Service Contract 1

InsMgmt5.gif

Service Contract 2

InsMgmt6.gif

Service 

InsMgmt7.gif 

  1. There are two contracts IService1 and IService2.
  2. For IService1 session mode is set to SessionMode=SessionMode.Required
  3. For IService2 session mode is set to

    SessionMode = SessionMode.NotAllowed
     
  4. Service is implanting both the contracts.
  5. Service Behavior is configured to single instance mode

    ServiceBehavior(InstanceContextMode    =InstanceContextMode.Single)]

    Web.Config

    InsMgmt8.gif

  6. In System.ServiceModel, we need to expose two addresses. One each for individual contracts.

Client

InsMgmt9.gif

Output

InsMgmt10.gif

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

  1. Having scalability problem.
  2. All requests to service will run on different worker thread, so it will cause synchronization problem.
  3. Only one client could have access to singleton service instance at a time. It will decrease throughput of the service.
  4. Responsiveness is very low
  5. 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. 

Up Next
    Ebook Download
    View all
    Learn
    View all