none
"System.TimeoutException" when calling WCF Service RRS feed

  • Question

  • Hi,

    I have developed a Web API-application that are consuming av WCF Service.
    This WCF method are returning a image back to the client, based on an ID parameter.

    My problem is that I receive the following exception when calling the WCF service method "GetImagesByActivityNumber":

    Message: [TimeoutException: The request channel timed out while waiting for a reply after 00:01:00. 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.]

    Here are the client code:

     public byte[] GetImages(string id)

            {

                byte[] sign = null;

                ImageData[] images = null;

     

                try

                {

     

                    var client = new ServiceClient();

     

                    images = client.GetImagesByActivityNumber(id);

     

                    try

                    {

                        client.Close();

                    }

                    catch (CommunicationException cex)

                    {

                        client.Abort();

                        throw;

                    }

                    catch (TimeoutException tex)

                    {

     

                        client.Abort();

                        throw;

                    }

     

                    catch (Exception ex)

                    {

     

                        client.Abort();

                        throw;

                    }

     

     

     

                    if (images != null)

                    {

                        var res = images.FirstOrDefault();

                        if (res != null)

                        {

                            sign = res.Data;

                        }

                    }

                     

     

                }

                catch (Exception ex)

                {

                    //logging

                }

          

                return sign;

           }

    ..and here are the web.config for the Client:

      <system.serviceModel>

        <bindings>  

          <wsHttpBinding>

            <binding name="BackendService1" maxReceivedMessageSize="2147483647">

              <readerQuotas maxArrayLength="2147483647"  maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647" />

              <security mode="None" />

            </binding>        

          </wsHttpBinding>

        </bindings>

        <Client>

          <endpoint address="http://XXX/Service.svc"

            binding="wsHttpBinding" bindingConfiguration="BackendService1"

            contract="XXX.BackendService"

            name="WSHttpBinding_BackendService">

            <identity>

              <userPrincipalName value="XXX" />

            </identity>

          </endpoint>     

        </client>

      </system.serviceModel>

    The WCF service har configured With  InstanceContextMode.PerCall and ConcurrencyMode.Multiple:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]

    The WCF Method code:

    public List<ImageData> GetImagesByActivityNumber(string id)

            {   var results = new List<ImageData>();

                try

                {

                    using (SqlConnection connection = new SqlConnection(string.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI; Min Pool Size={2}",

                            WebConfigurationManager.AppSettings["DataSource"], WebConfigurationManager.AppSettings["InitialCatalog"], WebConfigurationManager.AppSettings["MinPoolSize"])))

                    {

                        SqlCommand sqlcommand = new SqlCommand("SELECT XX, YY From XXData WHERE id = @id", connection);

                        sqlcommand.Parameters.AddWithValue("@id", id);

                        connection.Open();

                        SqlDataReader reader = sqlcommand.ExecuteReader();

                        if (reader.HasRows)

                        {

                            while (reader.Read())

                            {

                                var result = new ImageData

                                {   XX = (string)reader.GetValue(0),

                                    YY = (ImageFileType)((int)reader.GetValue(1))

                                };

                                results.Add(result);

                            }

                        }

                        reader.Close();

                    }   

                }

                catch (Exception ex)

                { //logging

                }

                 return results;

            }

    The WCF Service is running on IIS 8.5 (Windows Server 2012 R2)

    I don't know why System.TimeoutException keep coming. It's not too much service call coming through. I guess around 100 calls pr minute. It's not an option to increase the timeout tresholds.

    Hope someone can help me With this issue.

    Regards,

    Marius H. Enerud




    • Edited by morris80 Monday, April 18, 2016 11:42 AM
    Monday, April 18, 2016 11:39 AM

Answers

  • Hello,

    >>I guess the timeout has something to do with the WCF connection not being terninated soon enough

    Maybe we can close the WCF connection in code behind as following:

    YourClientProxy client = new YourClientProxy();try {

    // do something ... client.Close(); } catch (CommunicationException e) { ... client.Abort(); } catch (TimeoutException e) { ... client.Abort(); } catch (Exception e) { ... client.Abort(); throw; }

    Besides, if you think the timeout has something to do with the WCF connection, I will also recommend you use the Fiddler to monitor the network. Please try to compare the time the request is sent from the client with the time that the server receives the call. Also please compare the time the response is sent from the service with the time that the client receives the response. This will help you to figure out if the time is spent on the client side/service side or on the network transfer. If the time is spend on network transfer, you may want to check if your network connection is stable.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 20, 2016 6:36 AM
    Moderator

All replies


  • Ops! Forgot web.config for the WCF Service:

      <system.serviceModel>

        <bindings>

          <wsHttpBinding>

            <binding messageEncoding="Text"  

                     closeTimeout="00:10:00" 

                     openTimeout="00:10:00" 

                     receiveTimeout="00:10:00" 

                     sendTimeout="00:10:00" 

                     maxBufferPoolSize="2147483647" 

                     maxReceivedMessageSize="2147483647">

              <security mode="None">

              </security>

            </binding>

          </wsHttpBinding>

        </bindings>

        <client />

        <behaviors>

          <serviceBehaviors>

            <behavior>

              <serviceDebug includeExceptionDetailInFaults="true"/>

            </behavior>

            <behavior name="serviceBehavior">

              <serviceThrottling maxConcurrentCalls="1000"

                 maxConcurrentInstances="1000" maxConcurrentSessions="1000" />

              <dataContractSerializer maxItemsInObjectGraph="2147483647" />

            </behavior>

          </serviceBehaviors>

        </behaviors>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />

        <services>

          <service name="XXX.BackendService" behaviorConfiguration="serviceBehavior">

            <host>

              <baseAddresses>

                <add baseAddress="http://XXX/BackendService"/>

              </baseAddresses>

            </host>

            <endpoint address="http://XXX/BackendService.svc" binding="wsHttpBinding" contract="XXX.BackendService">

              <identity>

                <userPrincipalName value="XXX" />

                <servicePrincipalName value=""/>

              </identity>

            </endpoint>

          </service>

        </services>    

      </system.serviceModel>


    Monday, April 18, 2016 11:41 AM
  • Hello,

    >>TimeoutException: The request channel timed out while waiting for a reply after 00:01:00. 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.

    I see that you have increased the timeout value in the Service side, but you do not increase the timeout value in the Client side, so you may meet the above timeout exception, please increase the Client timeout as following:

       <wsHttpBinding>
           <binding name="BackendService1" 
    closeTimeout="00:10:00"
    openTimeout="00:10:00"
    receiveTimeout="00:10:00"
    sendTimeout="00:10:00" maxReceivedMessageSize="2147483647"> <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647" /> <security mode="None" /> </binding> </wsHttpBinding>

    >>don't know why System.TimeoutException keep coming. It's not too much service call coming through.

    Base on your description, I know that a image will be transferred in the service and client side, so maybe the image is too large, which causes WCF service spend much time to response it. Or maybe it spends much time to retrieve the image from database or somewhere. I will recommend you could create some logs on this I/O operations and check what step spends much time.

    Best regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 20, 2016 2:30 AM
    Moderator
  • Thanks for the answer! I already log the sql operation and it takes maximum 10-15 seconds to return data from SQL server. I guess the timeout has something to do with the WCF connection not being terninated soon enough. Maybe some configuration on the IIS? I have tried TraceViewer on the server without finding any errors. regards, Marius
    Wednesday, April 20, 2016 5:22 AM
  • Hello,

    >>I guess the timeout has something to do with the WCF connection not being terninated soon enough

    Maybe we can close the WCF connection in code behind as following:

    YourClientProxy client = new YourClientProxy();try {

    // do something ... client.Close(); } catch (CommunicationException e) { ... client.Abort(); } catch (TimeoutException e) { ... client.Abort(); } catch (Exception e) { ... client.Abort(); throw; }

    Besides, if you think the timeout has something to do with the WCF connection, I will also recommend you use the Fiddler to monitor the network. Please try to compare the time the request is sent from the client with the time that the server receives the call. Also please compare the time the response is sent from the service with the time that the client receives the response. This will help you to figure out if the time is spent on the client side/service side or on the network transfer. If the time is spend on network transfer, you may want to check if your network connection is stable.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 20, 2016 6:36 AM
    Moderator