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

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives