Thursday, 11 June 2015

Session Mode in WCF

This post is continuation of post Instance Context Mode in WCF. Please visit following post before reading this post.
http://logicsmaze.blogspot.in/2015/06/instance-context-mode-in-wcf.html

Code to retrieve session ID from the WCF service :
string sessionID = OperationContext.Current.SessionId;

Code to retrieve session ID from the client application :
string sessionID = ocjClient.InnerChannel.SessionId;

Client side and server side session id's are co-related using the reliable session id.

 <binding name="netTCP" receiveTimeout="00:00:20">
          <reliableSession enabled="true"/>
  </binding>

If TCP binding is being used with reliable session is disabled, then the client and server session id's will be different. if reliable session are enabled ,the session id's will be same.

With wsHttpbinding, the session id's are same whether reliable session are  enabled or not.

Session Mode in WCF :

There are three type of session mode enumerator in WCF that we can use with [ServiceContract].













Allowed : This is a default session mode if we does not specified explicitly.In this mode service contract support session if binding supports them.

NotAllowed : If we use this enumerator of session mode, service contract does not support binding that initiate sessions( TCP binding support session so we can't use netTcpBinding binding with this mode of session).

Required : service contract requires a binding that support session.If we use binding that does not support session like basicHttpBinding with session mode Required, following exception will throw.
System.InvalidOperationException : Contract requires session, but binding "BasicHttpBinding" does't support it.

Following are some example with combination of InstanceContextMode, SessionMode and bindings.

Example 1 : Set service InstanceContextMode to Single and SessionMode to Allowed

If we use basicHttpBinding that does not support sessions, the service still works as a singleton service but without session( Without session means if we try to get value of session it will be blank in both slient side and service side).

On the other hand if we use netTcpBinding that support sessions, the service gets a session and continue to work as a singleton service.

Example 2 : Now set SessionMode to Required

If we use netTcpBinding, that support sessions, the service gets a session and continue to work as a singleton service.

On the other hand, if we use basicHttpBinding that does not support sessions, the following exception is throws.
System.InvalidOperationException : Contract requires session, but binding "BasicHttpBinding" does't support it.

You can try more example by changing combination of values of InstanceContextModeSessionMode and bindings.

Related Post : 

Message-exchange-pattern-in-wcf
Different-option-of-hosting-wcf-service
Binding-in-wcf-choosing-right-wcf-service
Hosting WCF with Non-Http protocol
Exchanging-metadata-in-wcf
Some-interesting-facts-about-data-contract
Knowntype-attribute-in-wcf
Associating-knowntype-in-wcf
Message-contract-in-wcf
Exception-handling-in-wcf


Instance-context-mode-in-wcf


No comments:

Post a Comment