Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 39 - PerSession WCF services

Suggested Videos
Part 36 - Instancing modes in WCF
Part 37 - PerCall instance context mode in WCF
Part 38 - PerSession instance context mode in WCF

Many of you have posted the following questions about PerSession WCF services. So I thought I will clarify all those questions in this video. Here are the questions that we will discuss in this video.
1. Does all bindings in WCF support sessions?
2. How to control the WCF service session timeout?
3. What happens when the session timeout is reached?
4. How to fix, The communication channel is in a faulted state exception?

This is continuation to Part 38. Please watch Part 38 before proceeding. We will work with the same example, that we worked with in Part 38.

Does all bindings support sessions
No, not all bindings support sessions. For example basicHttpBinding does not support session. If the binding does not support session, then the service behaves as a PerCall service.

Please refer to the MSDN link below, to check which bindings do and donot support sessions

How to control the WCF service session timeout?
The default session timeout is 10 minutes. If you want to increase or decrease the default timeout value, 
Step 1: Set receiveTimeout attribute of the respective binding element as shown below. In the example below, we have configured the session timout for 10 seconds.
    <binding name="netTCP" receiveTimeout="00:00:10"></binding>

Step 2: Associate the binding element with the endpoint using bindingConfiguration attribute as shown below.
<endpoint address="SimpleService"

What happens when the session timeout is reached
When the session timeout is reached, the connection to the wcf service is closed. As a result, the communication channel gets faulted and the client can no longer use the same proxy instance to communicate with the service. This also means that along with the session, the data in the service object is also lost.

After the session has timed out, 
1. On the first attempt to invoke the service using the same proxy instance would result in the following exception.
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9355444'.

2. On the second attempt, the following exception will be thrown
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

How to fix, The communication channel is in a faulted state exception
1. Put the line that calls the service should in the try block
2. Catch the CommunicationException
3. Check if the communication channel is in a faulted state and create a new instance of the proxy class.

    MessageBox.Show("Number = " + client.IncrementNumber().ToString());
catch (System.ServiceModel.CommunicationException)
    if (client.State == System.ServiceModel.CommunicationState.Faulted)
        MessageBox.Show("Session timed out. Your existing session will be lost. A new session will now be created");
        client = new SimpleService.SimpleServiceClient();

wcf tutorial


  1. Dear Venkat sir,
    I am continuously watching your videos, It's worth above 1 lacks rupees.
    Thanks a lot. I want to learn SSRS, if It's possible, please upload.

  2. Dear Sir,

    I was going through Per-Session instance context mode video and found that if we don't mention servicebehavior as instance context modes then what is the default instance context mode set on the WCF service.

    With Thanks and Regards,
    Raghavendra Reddy


It would be great if you can help share these free resources