Thursday, August 16, 2007

* WCF : Service Instances and Sessions

Previous Post <<-- WCF : Understanding Data Contracts (Deep Dive)

WCF allows developers to decide on how WCF should create instances of Service Object in response to client calls. It can be controlled by applying design time behavior attribute "System.ServiceModel.ServiceBehaviorAttribute.InstanceContextMode" over service implementation.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class MathService : IMathService
{
    …
}

Following three instancing modes are available :

  1. PerCall : for each client call, a new service object is created.
  2. PerSession : If sessions are supported, a new object is created for each session and continues to service the client calls throughout the lifetime of session.
  3. Single : a single service object is created which handles all client requests from all clients throughout the lifetime of application.

The session related behavior of WCF service can also be controlled by design time behavior attribute "System.ServiceModel.ServiceContractAttribute.SessionMode". The values which can be set are : Allowed, NotAllowed and Required.

Sessions are initiated and terminated by calling client and there is no data store associated with sessions as in ASP.NET. Messages are processed in the order they are received.

If the instancing mode has been set as 'PerSession', only one Service object will be created for each session which will service the client throughout the lifetime of session.

Before getting more into sessions let me explain, What are Reliable Sessions ?

Reliable session makes sure that :

  • Each message gets delivered only once
  • Message are received by receiver in the same order as they have been sent.

When working with session aware communication there are two options available for developers to make sure that sessions created are reliable :

  • Use the transport binding which inherently provides reliable sessions like TCP.
  • Use WCF bindings which guarantee reliable sessions even over unreliable transport bindings like HTTP. The default system provided bindings which give an options of reliable sessions are :
    • WSHttpBinding
    • WSFederationHttpBinding
    • NetTCpBinding
    • WSDualHttpBinding

Here is a sample configuration which shows how to configure reliable session on WSHttpBinding

    <bindings>
<wsHttpBinding>
<binding name="BindingName">
<reliableSession enabled="true" ordered="true" />
</binding>
</wsHttpBinding>
</bindings>

 


Next Post -->> WCF : Understanding Security

 

No comments:

Post a Comment