Instance Management in WCF Part # 3

No.of Views2217
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  28 Aug 2010 00:08:00
Tag : WCF , General
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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

Image Loading

 

Image Loading
  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.
Image Loading

Code

 

Image Loading

Explanation with example

Service Contract 1

 

Image Loading

Service Contract 2

 

Image Loading

Service

 

Image Loading
  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
  4. SessionMode = SessionMode.NotAllowed;
  5. Service is implanting both the contracts.
  6. Service Behavior is configured to single instance mode
  7. ServiceBehavior(InstanceContextMode    =InstanceContextMode.Single)]

Web.Config 

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

Client

 

Image Loading

Output

Image Loading

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.

Sample Project Source

Download source files -55 kb

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top