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.

  • Open timeout: Time required to open the channel
  • Close timeout: Time required to close the channel

    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();
     
    Similarly increase the open timeout and closetime out on the service config file

     

    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.

     

  • 6 Comments

    • nice blog.. it solves my issue.. Thanks

    • I'm consistently getting a message very similar to this after 1 minute, which is plenty of time for the operation to complete. It used to work, and I don't know how I broke it. Even comparing the "before" and "after" configuration and source files, I have not been able to figure it out. I'm using Silverlight 4 and VS 2010, as I was when it worked. I was trying to make it use https, but I made other changes too. The client configuration is generated by VS 2010, but I've also tried many changes to ServiceReferences.ClientConfig based on articles I found surfing the Internet, but whatever I do, I get this message,except when I break something and don't get far enough for this message.

      Here is the exact message"

      The HTTP request to 'http://localhost:1581/TicketService.svc' has exceeded the allotted timeout. The time allotted to this operation may have been a portion of a longer timeout.
      ---> System.Net.WebException ---> System.Net.WebException
      at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
      at System.Net.Browser.BrowserHttpWebRequest.c__DisplayClass5.b__4(Object sendState)
      at System.Net.Browser.AsyncHelper.c__DisplayClass2.b__0(Object sendState)
      --- End of inner exception stack trace ---
      at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
      at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
      at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
      --- End of inner exception stack trace ---
      at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
      at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
      at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
      at Tickets.TicketServiceReference.TicketServiceClient.TicketServiceClientChannel.EndGetTypeTable(IAsyncResult result)
      at Tickets.TicketServiceReference.TicketServiceClient.Tickets.TicketServiceReference.ITicketService.EndGetTypeTable(IAsyncResult result)
      at Tickets.TicketServiceReference.TicketServiceClient.OnEndGetTypeTable(IAsyncResult result)
      at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)}

      Can anyone help. Thanks.

    • i find it useful here. Thank u so much.

    • Timeout exceptions.. Slap-up :)

    • you gave one of the best solution..
      Thanks

    • Each user has a corrupted database. Concurrently, Microsoft had total
      revenue of nearly $23 billion. The secondary
      function is to store and maintain data centers focus
      on how to accomplish it. Then simply type in the IP address of your Minecraft
      justhost and press connect. 5 Press Releases Yet another free way to spread the word about
      mobile Justhosts. Horse competitions also are minigames, and challenge
      players with eye-hand coordination, math knowledge, and approach.

    Comments have been disabled for this content.