WCF Instance Management


What is Instance Management?

Instance Management is the name for a set of techniques used by WCF to bind client requests to service instances, governing which service instance handles which client request. This is a service side implementation detail. Reference: Programming WCF Services

Why we need Instance Management

We need Instance Management because applications differ too much in their needs for scalability, performance, throughput, transactions and queued calls. So one solution for all applications doesn't fit.

3 ways of instance management

WCF framework has provided 3 ways by which we can manage WCF service instance creation.

  • Per-Call
  • Per-Session
  • Single

Per-Call instance mode

One instance of the service class is created per incoming request. A service instance is created only when the client call is in progress and it will be destroyed after response is sent back to client.

Every time the client sends a new request either from the same proxy or a different proxy, a new service instance is always created.

Per-Call instance mode in WCF

This represents the process of handling the request from client using Per-Call instance mode.

WCF Per-Call instance mode


Implementation of Per-Call services

To configure a service type as a per-call service, we need to apply the 'ServiceBehavior' attribute with the 'InstanceContextMode' property set to InstanceContextMode.PerCall.

---------------------------- Service code ---------------------------- 

    [ServiceContract
    interface IMyContract 
    { 
        [OperationContract
        int MyMethod( ); 
    } 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    class MyService : IMyContract,IDisposable 
    {
        static int m_Counter = 0;
        public int MyMethod( )
        { 
            m_Counter++; 
            return m_Counter; 
        }
    }
 

----------------------------- Client code --------------------------

        MyContractClient proxy = new MyContractClient( );
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.ReadLine();


--Possible Output

Counter: 1
Counter: 1
Counter: 1
Counter: 1

Important points of per-call instance mode

  • Service instance exists only when client call is in progress. It is allocated when a client makes a request and deallocated when response is sent back to the 
    client.

Per-Session instance mode

One instance of the service class is created per client session. Here, a logical session is maintained between client and service.

Each proxy will connect to new instance.

WCF Per-Session instance mode


Implementation of Per-Session services

To configure a service type as a per-session service, we need to apply the 'ServiceBehavior' attribute with the 'InstanceContextMode' property set to InstanceContextMode.PerSession.

---------------------------- Service code ---------------------------- 

    [ServiceContract
    interface IMyContract 
    { 
        [OperationContract
        int MyMethod( ); 
    } 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    class MyService : IMyContract,IDisposable 
    {
        static int m_Counter = 0;
        public int MyMethod( )
        { 
            m_Counter++; 
            return m_Counter; 
        }
    }
 

----------------------------- Client code --------------------------

        MyContractClient proxy = new MyContractClient( );
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.WriteLine("Counter: " + proxy.MyMethod());
        Console.ReadLine();


--Possible Output

Counter: 1
Counter: 2
Counter: 3
Counter: 4

Important points of per-session instance mode

  • Each proxy will be connected to a new dedicated service instance.

  • Service instance is allocated when client creates a new proxy and deallocated when client destroys this proxy object.

  • Client session is per service endpoint per proxy.

Single instance mode

One instance of the service class handles all incoming requests. This implements a singleton. When a service is configured as a singleton, all clients connect to the same shared service instance; it is not a factor, to which endpoint of the service they are connected.

All client requests will go to same instance.

WCF Single instance mode


Implementation of Single instance services

To configure a service type as a single instance service, we need to apply the 'ServiceBehavior' attribute with the 'InstanceContextMode' property set to
InstanceContextMode.Single.

---------------------------- Service code ---------------------------- 

    [ServiceContract
    interface IMyContract 
    { 
        [OperationContract
        int MyMethod( ); 
    } 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    class MyService : IMyContract,IDisposable 
    {
        static int m_Counter = 0;
        public int MyMethod( )
        { 
            m_Counter++; 
            return m_Counter; 
        }
    }
 

----------------------------- Client code --------------------------
       
        Console.WriteLine("Client 1 making call to service…. ");
        Console.WriteLine("Counter: " + proxy1.MyMethod());
        Console.WriteLine("Counter: " + proxy1.MyMethod());
        Console.WriteLine("Counter: " + proxy1.MyMethod());
 
        Console.WriteLine("Client 2 making call to service…. ");
        MyContractClient proxy2 = new MyContractClient( );
 
        Console.WriteLine("Counter: " + proxy2.MyMethod());
        Console.WriteLine("Counter: " + proxy2.MyMethod());
        Console.ReadLine();


--Possible Output

Client 1 making call to service....

Counter: 1
Counter: 2
Counter: 3

Client 2 making call to service....

Counter: 4
Counter: 5

Important points of Single instance mode

  • Only one service instance is created. It is created when the service host is created.

  • This service instance lives forever and is only destroyed when service host shuts down.

  • Only one service instance is created. It doesn't matter whether the requests coming from different endpoints or different proxies.

When to use per-call, per-session and single instance mode

Different instance mode in WCF

Up Next
    Ebook Download
    View all
    Learn
    View all