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
-
WCF can maintain a logical session between a client and service.
-
The service instance remains in the memory throughout the session.
-
The client session is per service endpoint per proxy.
Configuring Session Full service
There are three elements, which must be taken care while configuring a session full service.
Behavior Facet
-
The Behavior part is required, so that WCF will keep the Service Instance Context alive throughout the session.
-
InstanceContextMode.PerSession is default value for InstanceContextmode property.
-
The session terminates when client closes the proxy.
-
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.
-
For this ServiceContract attribute having a property called SessionMode.
-
Type of property SessionMode is enum SessionMode.
-
It is inside namespace System.ServiceModel.
-
Property
SessionMode is a public property.
-
SessionMode enum is default to SessionMode.Allowed.
-
The Configured value of SessionMode is exposed to client in
MetaData.
SessionMode.Allowed
-
When the SessionMode is configured with SessionMode.Allowed, transport session are allowed but not enforced.
-
The Behavior of service will depend upon behavior configuration and binding.
Code Example
Both code is same because SessionMode.Allowed is default .
SessionMode.Required
-
SessionMode.Required value restricts to use Transport level session.
-
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.
Code Example
SessionMode.NotAllowed
-
This disallows the use of Transport session.
-
Regardless of the service configuration, when SessionMode.NowAllowed is configured at contract, the service will always behave as Per-call service.
Code Example
Explanation of Per-Session service instance with a Sample
Consider code below
Service contract
Service
Client
Output
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.