locked
System.Net.WebException: "The operation has timed out"

    Question

  • I know this has been asked umpteen times but i cant find an answer to my problem.

    Very simply, i have a webservice client  which is trying to call a webservice. The webservice may take any time from 5mins to 30mins (or more) for downloading message data  from pop3 server . However after 2mins i get the following error:

    System.Net.WebException: The operation has timed-out

    This is an issue with the webservice client timing out calling the webservice - as
    the underlying webservice still runs in the background

    Sequence osf steps when the error occurs:

    1.User logins with his credentials

    2.Webservice client calls webservice to connect to pop3 server and download messages for that particular user.

        Code in webservice for downloading data from pop3 server where the exception occurs

        private string ReadOutput(bool bMultiline)
        {
            try
            {
               
                StreamReader srReader = new StreamReader(conn);
                string strRet = srReader.ReadLine();
                if (bMultiline)
                {
                    // Example #4: Append new text to an existing file
                    string strLine = "";
                    while (true)
                    {
                        // In a multiline message the . indicates end of message
                        if (strLine == ".")
                            break;
                        // Relay will convert any line with just a . in it to ..
                        if (strLine == "..")
                            strLine = ".";
                        strRet += "\r\n" + strLine;                                        
                    }               
                }
                return strRet;
            }
            catch (Exception e)
            {
                return null;
            }
        }

    3.Exception is caught in webservice client while webservice continues to run.This exception occurs only when the user receives a large message may be above 1mb because i tested with  1MB  msg file where i dint have any problem receiving or downloading from pop3 server where as with 5 MB message file i got this exception .

    After going through couple of forums i tested my luck by changing web.config in both webservice and webservice client  where  i set the attribute of httpruntime Execution timeout to 300 seconds with no success <httpRuntime executionTimeout="300"></httpRuntime>.

    Guys Any Ideas in solving this issue.

    Note: I have this webservice client and webservice running on localmachine(OS-Windows Vista)

    Thank you

     

     

      

     

     

     

    Monday, May 03, 2010 8:38 PM

Answers

  • Hi,

    Please try to set TimeOut property at the client. I will encourage you to use WCF for future development.

    Service1 client = new Service1();
          client.Timeout = 999999;
          string msg = client.HelloWorld();
          Console.WriteLine(msg);

     Hope this helps,

    Neha Garg

    Tuesday, May 04, 2010 6:03 PM
    Moderator

All replies

  • Have you tried to change SendTimeOut and ReceiveTimeOut property at the binding? You can change these two properties in the web.config.

    Thanks,

    Neha Garg

    Tuesday, May 04, 2010 12:15 AM
    Moderator
  • I just want to clear that Iam not using WCF ,the webservice I am using is ASMX based Webservice and client is just a simple web app which is trying to download messages for particular user from POP3 Server.I simply added the web references of the webservice to Web App project and trying to invoke webservice methods from main webapp and it works for small size file but it throws an exception when trying to download large files.

    More Debug Information remember the exception occurs at client side which is a simple web app.

    Stack Trace:"   at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)\r\n   at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)\r\n   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)\r\n   at RelayService.RelayService.GetMessages(RelaySettings rsSettings) in c:\\Users\\RCirigiri\\AppData\\Local\\Temp\\Temporary ASP.NET Files\\web apps\\6be971d4\\14e427f6\\App_WebReferences.3bbi7i5g.1.cs:line 278\r\n   at PageBase.DownloadEmails(String strSessionKey) in c:\\2.00.07\\Source Code\\Web Apps\\App_Code\\PageBase.cs:line 226"

    Message:"The Operation has Time Out"

    Source:"System.Web.Services" 

    Any Ideas?.Whts going on?..

     

    Tuesday, May 04, 2010 12:46 PM
  • Can you try to set TimeOut property at the client side?  Please use WCF for your future development.

    Service1

     

     

     

    client= new Service1(); client.Timeout = 9999999;

     

     

    Hope this helps,

    Neha Garg

     

     

    string msg = client.HelloWorld();
    Tuesday, May 04, 2010 5:59 PM
    Moderator
  • Hi,

    Please try to set TimeOut property at the client. I will encourage you to use WCF for future development.

    Service1 client = new Service1();
          client.Timeout = 999999;
          string msg = client.HelloWorld();
          Console.WriteLine(msg);

     Hope this helps,

    Neha Garg

    Tuesday, May 04, 2010 6:03 PM
    Moderator
  • I did that already it worked but it took 15-20 minutes for  downloading a message from POP3 Server then after looking through the code where it was reading network stream and building the message i finally caught the culprit it was String Object.Using String Object for string concatenation created all the trouble Every time you add two strings using + operator, a new string allocation is reserverd in memory and used for this new string. So if we are adding strings 100 times using + operator, there will be 100 new memory allocations and this was the causing delay response from webservice  which in turn times out the webservice client. Instead of String Object i used  StringBuilder where there will only be one memory allocation which helped in getting fatser response from webservice.Replacing String object with StringBuilder object helped in solving this case. This web app was developed was developed few years back so they used asmx based webservice but yeah for future project we will definitely use WCF.

    Thanks Neha

     

     

                

     

    Tuesday, May 04, 2010 8:14 PM
  • FYI, you should never use the "+" operation to concatenate strings, unless there are always very few of them. Any other time, you should use StringBuilder, as you have discovered.

    This issue is by-design, and has been well-known since .NET 1.0 (it's the reason that StringBuilder exists), and is not specific to web services.


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, May 04, 2010 10:53 PM
    Moderator