Part 39 PerSession WCF services

Published on January 7, 2018

Link for code samples used in the demo

Link for all dot net and sql server video tutorial playlists

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.

Step 2: Associate the binding element with the endpoint using bindingConfiguration attribute.

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.

Example:
private void button1_Click(object sender, EventArgs e)
{
try
{
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();
}
}
}

Tags:,

https://cafeadobro.ro/

https://www.stagebox.uk/wp-includes/depo10-bonus10/

depo 25 bonus 25

https://parfumschristianblanc.com/

https://www.barplate.com/wp-includes/js/qris/

https://hotmusic507.org/

Enjoyed this video?
"No Thanks. Please Close This Box!"