Different ways to maintain WCF instance management

The WCF framework has provided three ways by which we can control WCF instance creation. Here will try to understand these ways of WCF service instance control and compare when to use them and under what situations.

Normally WCF request and response consists of the following actions:

1.       WCF client makes a request to a WCF service object.
2.       WCF service object is instantiated.
3.      WCF service instance serves the request and sends the response to the client.

There are different ways to instantiate WCF service object. In a simple way, create a new WCF service instance whenever WCF client method calls. Only one WCF service instance should be created for each WCF client session. And third way is only one global WCF service instance should be created for all WCF clients. WCF provides the ways to handle the above three ways:
      
  •       Per call
  •       Per session
  •       Single instance


Per Call

By default WCF is using "Per-Call", which means new instance of the WCF service is created for each client's call. When we configure a WCF service as per call, new service instance is created for every method call of WCF client. In simple words, for every WCF client method call, a WCF service instance is created, and destroyed once the request is served.

Sample Code:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Percall)]
public class Service : IService
{
    // Your Code
}

Per Session

In some situations, we need to maintain state between method calls or for a particular session. For those kinds of scenarios, we need to configure the service per session. In per session, only one instance of a WCF service object is created for a session interaction. WCF service object is disposed once the session is expired.

Sample Code:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service : IService
{
    // Your Code
}

Single

When WCF service is configured for Singleton instance mode, all clients are independently connected to the same single instance. This singleton instance will be created when service is hosted and, it is disposed when host shuts down.

Sample Code:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
    // Your Code
}

When should we use these three ways:

Per call
·         In a stateless services situation.
·     WCF service holds intensive resources like connection objects and huge memory objects.
·         In a situation where scalability is a prime requirement.
·         WCF functions are called in a single threaded model.

Per session
·         When need to maintain states between WCF calls.
·         In a scaled up architecture.
·         Light resource references in WCF service object.

Single
·         Need to share global data through WCF service.
·         Scalability is not a concern.


Comments

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options