Timeout exceptions
1. WCF
In a typical WCF client-service application, the client initiates the requests and waits for the response from service. If the service doesn’t respond in the preconfigured intervals the client will error out with an exception similar to the following exception:
System.TimeoutException: The request channel timed out while waiting for a reply after 00:00:00.1718750. 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.TimeoutException: The HTTP request to 'http://localhost:8888/SaveProducts' has exceeded the allotted timeout of 00:00:00.7810000. The time allotted to this operation may have been a portion of a longer timeout. ---> System.Net.WebException: The operation has timed out
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
There are four parameters that you can configure to avoid timeout: open timeout, close timeout, send timeout, receive timeout.
The parameters open timeout and close timeout are same in both the client and service configurations.
Case: Client sends a requests and waits for the service to respond
On the client side
- Send timeout: Time required to send the message to the service and receive the response
- Receive timeout: Not applicable on client side
On the service side
- Send timeout: Not applicable
- Receive timeout: Not applicable on
We can overwrite the time intervals either through web.config or programmatically in the code
Find the client’s web.config as an example
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="AdventureworksWsHttpBinding"
maxReceivedMessageSize="100000000" sendTimeout="00:10:00"
transactionFlow="true"
>
<reliableSession enabled="false" />
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8888/SaveProducts"
binding="wsHttpBinding" bindingConfiguration="AdventureworksWsHttpBinding"
contract="Adventureworks.Service.ISaveProducts"
name="Adventureworks" />
</client>
</system.serviceModel>
The following snippet shows how to overwrite using the code
ChannelFactory<ISaveProducts> channelFactory = new ChannelFactory<ISaveProducts>("Adventureworks");
channelFactory.Endpoint.Binding.SendTimeout = new TimeSpan(0,0,10,0);
service = channelFactory.CreateChannel();
2. IIS
If you have deployed your services on IIS don’t forget increase execution time from the default 1 min 50 seconds
<system.web>
<httpRuntime executionTimeout="600"/>
</system.web>
3. Database (SQL Server specific)
On the database front, the requests can timeout either for failure to open the connection or do the operation.
Connection timeout: The time taken to open the connection to the database
You can overwrite the default connection timeout (15 seconds) using ‘Connect Timeout = 30’ as part of the connecting string parameters.
Or can overwrite programmatically, SqlConnection.ConnectionTimeout
Command timeout: The time taken to execute the command on the database
SqlCommand.CommandTimeout can be used to overwrite the command timeout.