Instance Management in WCF Part # 2

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

WCFSession1.gif

WCFSession2.gif 

  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.

    WCFSession3.gif

Configuring Session Full service

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

WCFSession4.gif


Behavior Facet

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

    WCFSession5.gif

    WCFSession6.gif
     
  2. InstanceContextMode.PerSession is default value for InstanceContextmode property.
  3. The session terminates when client closes the proxy.
  4. 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.

WCFSession7.gif 

  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. 

    WCFSession8.gif
     
  5. SessionMode enum is default to SessionMode.Allowed.
  6. The Configured value of SessionMode is exposed to client in MetaData.

    WCFSession9.gif

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.

    WCFSession10.gif

Code Example

Both code is same because SessionMode.Allowed is default .

WCFSession11.gif

WCFSession12.gif

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.

    WCFSession13.gif

    WCFSession14.gif

Code Example

WCFSession15.gif

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.
    WCFSession16.gif

Code Example

WCFSession17.gif

Explanation of Per-Session service instance with a Sample


Consider code below

Service contract

WCFSession18.gif

Service

WCFSession19.gif

Client

WCFSession20.gif

Output

WCFSession21.gif

Explanation

  1. We could see from output is that Service Instance is particular and individual for each individual proxy.
  2. 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.