27 March 2013

Timeouts in WCF service

I was getting my head around an exception I got after calling a method on my local WCF service:

"The request channel timed out while waiting for a reply after 00:00:59.9990000. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout."

Obviously, the first thing I did was to increase a timeout in the web.config, guess what , no impact on the results.

So here is the summery after googling and debugging:

Availbale timeouts

Binding Timeouts

SendTimeout, ReceiveTimeout, OpenTimeout and CloseTimeout. Default = 60 sec.

<bindings>
   <netTcpBinding>
     <binding name="longTimeoutBinding"
               receiveTimeout="00:10:00" sendTimeout="00:10:00">
     <security mode="None"/>
    </binding>
   </netTcpBinding>
</bindings>

Why changing SendTimeout does not help for hosted WCF services?

The timeout configuration needs to be set at the client level, so the configuration I was setting in the web.config had no effect, the WCF test tool has its own configuration and there is where you need to set the timeout.

ServiceHost Timeout

OpenTimeout (default is 1 minute) , CloseTimeout (default 10 sec)

ServiceHost host = new ServiceHost(typeof(FileService), new Uri("https://localhost:8443/FileService"));

host.OpenTimeout = TimeSpan.FromMinutes(5);

host.CloseTimeout = TimeSpan.FromMinutes(5);

Timeouts on client side channel

This is somewhat not as well known as the previous ones.
There is an OperationTimeout, which you can set it by casting the channel to IContextChannel. You can do it through code. The default for this is also 1 minute.

IFileService channel = ChannelFactory<IFileService>.CreateChannel(binding, new EndpointAddress("https://localhost:8443/FileService"));

IClientChannel contextChannel = channel as IClientChannel;

contextChannel.OperationTimeout = TimeSpan.FromMinutes(10);

This is the exception you will get if you are hitting this operation timeout:
"Unhandled Exception: System.TimeoutException: The request channel timed out attempting to send after 00:00:00.0100000. Increase the timeout value passed to the
call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout. ---> System.T
imeoutException: The HTTP request to 'https://localhost:8443/FileService' has exceeded the allotted timeout of 00:00:00. The time allotted to this operation may
have been a portion of a longer timeout."

There is another not so well known timeout on tcp transport, called ChannelInitializationTimeout, and its default value is 5 seconds.

BindingElementCollection be = binding.CreateBindingElements();

TcpTransportBindingElement tcpBe = be.Find<TcpTransportBindingElement>();

tcpBe.ChannelInitializationTimeout = TimeSpan.FromMinutes(1);

CustomBinding customBinding = new CustomBinding(be);

Exception related to ChannelInitializationTimeout could look like:

"Unhandled Exception: System.ServiceModel.CommunicationException: 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.9257860'. ---> System.Net.Socke
ts.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.ServiceModel.Channels.SocketConnection.Write(Byte[] buffer, Int32 offset, Int32 size, Boolean immediate, TimeSpan timeout)
--- End of inner exception stack trace ---"

Timeouts in ASP.NET


If you are web hosted, you should consider hitting timeouts set by ASPNET. There are Shutdown timeout, just like the service host close timeout, default is 90 seconds. Execution Timeout, just like our operation timeout, default is 110 seconds.

<configuration>
<system.web>
<httpRuntime maxRequestLength="4000"
enable = "True"
requestLengthDiskThreshold="512
shutdownTimeout="90"
executionTimeout="110"
versionHeader="1.1.4128"/>
</system.web>
</configuration>

Search This Blog

Total Pageviews