Condividi tramite


Timeouts in WCF and their default values

 

There are a lot of timeouts in WCF. let us summarize it here.

Timeouts on binding

These are the most well known timeouts. SendTimeout, ReceiveTimeout, OpenTimeout and CloseTimeout. They can be set easily either through config or code on the Binding. The default value for those are 1 minute. 

E.g in code

Binding

binding = new NetTcpBinding(SecurityMode.Transport)

{

SendTimeout =

TimeSpan.FromMinutes(10),

ReceiveTimeout =

TimeSpan.FromMinutes(10),

OpenTimeout =

TimeSpan.FromMinutes(10),

CloseTimeout =

TimeSpan.FromMinutes(10),

};

You will see some timeout exception such as

 

Unhandled Exception: System.TimeoutException: The open operation did not complete within the allotted timeout of 00:00:00.0010000. The time allotted to this operation may have been a portion of a longer timeout. ---> System.TimeoutException: Open timed out after 00:00:00 while establishing a transport session to net.tcp://localhost:8080/FileService. The time allotted to this operation may have been a portion of a longer timeout. ---> System.TimeoutException: Connecting to via net.tcp://localhost:8080/FileService timed out after 00:00:00. Connection attempts were made to 0 of 2 available addresses (). Check the RemoteAddress of yourchannel and verify that the DNS records for this endpoint correspond to valid IP Addresses. The time allotted to this operation may have been a portion of a longer timeout.

Timeouts on ServiceHost

ServiceHost has OpenTimeout and CloseTimeout. Default for OpenTimeout is 1 minute, and default for CloseTimeout is 10 seconds.

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 ASPNET

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. ExecutionTimeout, 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>

For more details on the ASPNET related timeout, please read https://msdn.microsoft.com/en-us/library/e1f13641.aspx