Wednesday, 18 September 2013

Instance Management :: WCF


ServiceBehavior- Specifies the internal execution behavior of a service contract implementation.
ServiceBehaviour attribute is used to specify the InstanceContextMode for the WCF Service class.
(This can be used to maintained a state of the service or a client too)

There are three instance Context Mode in the WFC ::

PerSession : This is used to create a new instance for a service and the same instance is used for all method for a particular client. (eg: State can be maintained per session by declaring a variable)


PerCall : This is used to create a new instance for every call from the client whether same client or different. (eg: No state can be maintained as every time a new instance of the service is created) 


Single : This is used to create only one instance of the service and the same instance is used for all the client request. (eg: Global state can be maintained but this will be applicable for all clients)


Instance mode can be configured using ServiceBehavior attribute. This can be specified at implementing the service contract as shown below.

   [ServiceContract]
    public interface IService1
    {
        [OperationContract(SessionMode.Allowed)]
         int Increment();
    }
 ---------------------------------------------------------------------------------------------
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Percall)]//PerSession, Single
    public class Service1 : IService1
    {
     private int intCounter;

        public int Increment()

                 {
            intCounter++;
            return intCounter;
           }
       }

A note on SessionMode 

In the service contract we can also specify a property for configuring SessionMode. This property specifies how the sessions will be created on client requests. We can use this property to take control over the session creation for our WCF services.

There are three possible values for this property.
·         Allowed
·         NotAllowed
·         Required


The Allowed option specifies that this service will accept request from both sessionful and sessionless clients. Having sessions is not mandatory but if the clients want to work in sessionful manner, it is allowed. This mode is the default mode for WCF services. 

The NotAllowed option specifies that the service will not work in sessionful mode i.e. it will only accept requests from the sessionless/stateless clients. In case any sessionful/stateful client tries to make a request, an exception will be thrown.

The Required option states that the service will only work in sessionful environment i.e. only sessionful/stateful clients can request the service operations. In case of stateless clients making any requests, an exception will be thrown. 

These modes are important because the resulting behavior of the the WCF service will depend on the options selected for InstanceContextMode in ServiceBehavior and SessionMode in ServiceContract. So these options must be set carefully with the desired behavior in mind.

0 comments:

Post a Comment

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)