Dot Net For All

WCF Service Instance- PerCall, PerSession, Single

Do you know we can control the instance creation of WCF service class? Yes, in WCF we can control the WCF service instance unlike WebAPI.

In this article I will discuss different instance modes provided by the WCF framework. I will use the C# language for the examples in the post.

WCF Service Instance

Lets discuss all the three modes of instancing with examples

PerCall Instancing

Lets see an example of per call instancing. I will use the service which I have created in one of my previous article about WCF service. I have used the netTcpBinding for all the examples in this article.

For demo purpose I have changed the code of the service class as shown below

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MyServiceImplementation : IMyService
    {       
        public string Hello(PersonData person)
        {
            return "Hello " + person.FirstName + " My Hash Code is: " + GetHashCode();
        }
    }

In the code above I am returning the HashCode of the class along with the message. And added the ServiceBehavior attribute for the service with instance context mode as PerCall. To add the ServiceBehaviour attribute we have to include a reference of System.ServiceModel assembly.

HashCode of the each instance of a class will be different. If the instances are same the hash code will be same.

In the client I will call the same method using the same instance of the proxy as shown below.

            PersonData person = new PersonData() { FirstName = "Vikram" };
            MyServiceProxy proxy = new MyServiceProxy();
            Console.WriteLine(proxy.Hello(person));
            Console.WriteLine(proxy.Hello(person));
            Console.Read();

The output of the above code is shown in the below figure.

PerCall WCF Instance

As seen in the above result. Fo reach call from the call, a new instance of the service is getting created.

PerSession Instancing

  1. Class – scoped variables can exist and maintain their state across multiple calls.
  2. If we need to update the state, we need locking. Please check this article about Thread synchronisation and locking mechanism in .NET

Following protocols support the PerSession WCF Service Instance.

  1. TCP Binding
  2. IPC Binding
  3. Ws-Http binding with reliability or security turned on.
  4. If it is simple httpBinding or WS-Http binding without reliability or security turned on it is down graded to Per-call instancing.

I have changed the code of the Client as following and changed the ServiceBehaviour attributes InstanceContextMode to PerSession

            PersonData person = new PersonData() { FirstName = "Vikram" };
            MyServiceProxy proxy = new MyServiceProxy();
            Console.WriteLine(proxy.Hello(person));
            Console.WriteLine(proxy.Hello(person));


            MyServiceProxy proxy1 = new MyServiceProxy();
            Console.WriteLine(proxy1.Hello(person));

In the above code I have created two instances of the proxy. Lets see the output.

PerSession Instance

As we can see in the figure. First two calls are made using the same instance of the proxy, that is why they are returning the same hast code. The next call is made using the different instance, it returns a new hash code confirming our belief about the per session instancing.

Single Instancing

I have kept the client code same as from the example of the PerSession and changed the InstanceContextMode = InstanceContextMode.Single for service.

Check the below figure for the result.

Single Instance

As we can see the hash code is same for all the calls. Signifying that the same WCF Service Instance is serving the requests. Even of we call the service instance from different clients we will get the same instance.

Conclusion:

In this article I have discussed the WCF Service Instance and how it can be controlled using the ServiceBehaviour attribute class. I have discussed all the three ways of creating the service instance i.e. PerCall, PerSession and Single with examples.

 

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview