Instance Management in WCF Part # 2

No.of Views2766
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  02 Aug 2010 11:08:07
Tag : WCF , General
This article is part # 2 of Instance Management in WCF. This article will explain Session Full Instance management service. This will explain different Session Mode at Contract level. This will explain Per-session service also. This article will be explaining Session Full Service with a code also.
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 # 2 of Instance Management in WCF. This article will explain Session Full Instance management service. This will explain different Session Mode at Contract level. This will explain Per-session service also. This article will be explaining Session Full Service with a code also.

Part # 1 of this series could be found Here

Per-Session Service

Image Loading
Image Loading
  1. WCF can maintain a logical session between a client and service.
  2. The service instance remains in the memory throughout the session.
  3. The client session is per service endpoint per proxy. 
Image Loading

Configuring Session Full service

There are three elements, which must be taken care while configuring a session full service. 

Image Loading

Behavior Facet

  1. The Behavior part is required, so that WCF will keep the Service Instance Context alive throughout the session.
  2. Image LoadingImage Loading

  3. InstanceContextMode.PerSession is default value for InstanceContextmode property.
  4. The session terminates when client closes the proxy.
  5. Proxy has to notify the service that session has been closed and service calls the Dispose () method on worker thread.

Contract Facet

In order to create all messages from a particular client to a particular instance WCF needs to identify the client and this is done by TRANSPORT SESSION

Image Loading
  1. For this ServiceContract attribute having a property called SessionMode.
  2. Type of property SessionMode is enum SessionMode.
  3. It is inside namespace System.ServiceModel.
  4. Property SessionMode is a public property.
  5. Image Loading

  6. SessionMode enum is default to SessionMode.Allowed.
  7. The Configured value of SessionMode is exposed to client in MetaData. 
Image Loading

SessionMode.Allowed

  1. When the SessionMode is configured with SessionMode.Allowed, transport session are allowed but not enforced.
  2. The Behavior of service will depend upon behavior configuration and binding.
Image Loading

Code Example

Both code is same because SessionMode.Allowed is default . 

Image Loading
Image Loading

SessionMode.Required

  1. SessionMode.Required value restricts to use Transport level session.
  2. If a contract is configured as SessionMode.Required with a service end point who's binding does not maintain a Transport level session a Run time error will encountered at loading of service.
Image Loading
Image Loading

Code Example 

Image Loading

SessionMode.NotAllowed

  1. This disallows the use of Transport session.
  2. Regardless of the service configuration, when SessionMode.NowAllowed is configured at contract, the service will always behave as Per-call service.
Image Loading

Code Example 

Image Loading

Explanation of Per-Session service instance with a Sample

Consider code below,

Service contract 

Image Loading

Service  

Image Loading

Client 

Image Loading

Output 

Image Loading

Explanation

  • We could see from output is that Service Instance is particular and individual for each individual proxy.
  • When proxy is getting closed or down, service instance is also being disposed on worker thread.

Conclusion

This article was part# 2 of multi series Instance Management Article. This articles explained about Session Full Service Management.

Sample Project Source

Download source files -51 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
</