.NET Framework Developer Center > .NET Development Forums > .NET Framework Networking and Communication > System.Net.WebException: The request was aborted: The request was canceled.
Ask a questionAsk a question
 

AnswerSystem.Net.WebException: The request was aborted: The request was canceled.

  • Wednesday, February 01, 2006 9:33 AMManuel9999 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello,

    we very often get the above Exception on our application, which makes strong use of WebServices. Does anybody know, why this Exception can occure and how to avoid it?

    Kind regards,
    Manuel

Answers

  • Thursday, February 02, 2006 7:17 PMJonCole - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Are you setting a timeout on the WebService call?  Can you add code to see what amount of time elapses between the time you start the call and when you get the exception? 

    I would expect the error message to state that a timeout happened if it were a timeout causing the problem but I want to make sure this is not the cause. 

  • Friday, October 27, 2006 11:30 AMManuel9999 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The solution was to disable HTTP Keep Alive for posting the Requests.

All Replies

  • Thursday, February 02, 2006 12:32 AMJonCole - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you send the output of webException.ToString()?
  • Thursday, February 02, 2006 6:56 AMManuel9999 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I implemented the following function to debug the WebException:

    private void HandleWebException(System.Net.WebException ex)
    {
       string logEntry = "No response from Web Service " + Environment.NewLine + ex.ToString();
       logEntry +=
    Environment.NewLine + "Status: " + ex.Status;
       if (ex.InnerException != null)
       {
          logEntry +=
    Environment.NewLine + "InnerException: " + ex.InnerException.ToString();
       }
       if (ex.Response != null)
       {
          logEntry +=
    Environment.NewLine + "Response.ContentLength: " + ex.Response.ContentLength;
          logEntry +=
    Environment.NewLine + "Response.ContentType: " + ex.Response.ContentType;
          if (ex.Response.Headers != null && ex.Response.Headers.Count > 0)
          {
          foreach (string s in ex.Response.Headers.AllKeys)
          {
             logEntry +=
    Environment.NewLine + "Response.Headers."+s+": " + ex.Response.HeadersSleep;
          }
       }
       i
    f (ex.Response.ResponseUri != null)
       {
          logEntry +=
    Environment.NewLine + "Response.ContentType: " + ex.Response.ResponseUri.ToString();
       }
    }

    And this is the (poor  ) output:

    System.Net.WebException: The request was aborted: The request was canceled.
       at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
       at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
       at BEWebServices.BBAServiceService.triggerDateOverview(BBADateOverviewRequest in0) in C:\TFSProjects\BEFrontend\BEWebServices\BBAService.cs:line 47
       at BEWebServiceConverter.BbaConverter.TriggerOutboundFlightOverview(AdvertisedFlightRequest request, State state) in C:\TFSProjects\BEFrontend\BEWebServiceConverter\BbaConverter.cs:line 45
       at BEBusinessLogic.Commands.BbaCommand.TriggerOutboundFlightOverview() in C:\TFSProjects\BEFrontend\BEBusinessLogic\Commands\BbaCommand.cs:line 300
       at BEBusinessLogic.Commands.BbaCommand.Execute() in C:\TFSProjects\BEFrontend\BEBusinessLogic\Commands\BbaCommand.cs:line 45
       at BEBusinessLogic.Commands.DispatcherCommand.Execute() in C:\TFSProjects\BEFrontend\BEBusinessLogic\Commands\DispatcherCommand.cs:line 133
    Status: RequestCanceled

  • Thursday, February 02, 2006 7:17 PMJonCole - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Are you setting a timeout on the WebService call?  Can you add code to see what amount of time elapses between the time you start the call and when you get the exception? 

    I would expect the error message to state that a timeout happened if it were a timeout causing the problem but I want to make sure this is not the cause. 

  • Friday, October 27, 2006 11:30 AMManuel9999 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The solution was to disable HTTP Keep Alive for posting the Requests.
  • Monday, March 19, 2007 7:00 AMAzam Abdul Rahim Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    and how do you do that? can you please explain?
  • Monday, March 19, 2007 10:49 PMJonCole - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    HttpWebRequest.KeepAlive = false;
  • Thursday, March 29, 2007 11:55 AMAzam Abdul Rahim Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    For the benefit of those who may encounter this problem in the future, here is a more detailed explanation of what needs to be done.
     
    When we add a web reference to a project, VS will generate a file named Reference.cs for the web reference. This file defines a class which inherits the System.Web.Services.Protocols.SoapHttpClientProtocol, and the class contains methods that correspond to the web service methods available on the web reference.
     
    What we need to do is to edit this file and override the inherited GetWebRequest method. The overriding method should look something like this (sample written in C#):
     
    Code Snippet
            protected override WebRequest GetWebRequest(Uri uri)
            {
                HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest(uri);
     
                webRequest.KeepAlive = false;
                webRequest.ProtocolVersion = HttpVersion.Version10;
                return webRequest;
            }

     

    And remember that when you do an Update Web Reference command, VS will regenerate the file and your changes will be lost. So you will need to override the method again after each update.
  • Thursday, March 29, 2007 12:07 PMManuel9999 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Also a very good practice is to override the GetWebRequest Method in a base class and change the automatic generated proxy classes to inherit from this base class.

     

    Example base class:

     

    public abstract class BaseService : System.Web.Services.Protocols.SoapHttpClientProtocol

    {

      protected override System.Net.WebRequest GetWebRequest(Uri uri)

      {

          System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

          webRequest.KeepAlive = false;

          return webRequest;

       }

    }

  • Tuesday, April 10, 2007 6:13 PMMark6 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Manuel9999,

    Thanks for posting your code however I have another question.

    If I have an aspx 2.0 page that via a vb code behind calls a web service like

    Dim objWS as new WS.WS1

    strReturn = objWS.function1

    but I am not sure how to reference your code. I don't see references.vb in my project and from where I am making this call I already have an inherits statement for something else.

     

    Any help would be appriecated.

    Thanks

  • Thursday, May 10, 2007 10:37 PMRory Bannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The easiest way to handle this is to use partial classes.

        public partial class SubscriberEvent
        {
            protected override WebRequest GetWebRequest(Uri uri)
            {
                HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest(uri);

                webRequest.KeepAlive = false;
                return webRequest;
            }
        }

    Makes things much easier.
  • Saturday, May 12, 2007 12:56 AMMark6 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have a solution to my problem. To recap I have a asp.net 2.0 web application that is making a web service call to a web service hosted by tomcat version 5.5.12. Sometimes I would get the error noted above and it was all related to a configuration setting. .Net 2.0 uses HTTP 1.1  keep alives so that the tcp handshake doesn't need to be established every time a call is made, thus making things faster. However the default http keep alive timeout value in .net is 100 seconds and in my case tomcat's http keep alive timeout was set less than that so tomcat would expire the http keep alive and then when .net would go to use that thread is would be broken thus the thread abort message.

     

    The real solution was to configure tomcat to expire the http keep alives after .net. With this you get the extra performance of http keep alives and no headaches of abort errors.

     

    Hope this helps.

    Mark

  • Thursday, May 24, 2007 1:09 PMPAsp Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    How do I change the keep alive timeout on Tomcat?

    I only found the timout on Tomcat version 6 and above.

    /Peter

  • Thursday, May 24, 2007 5:33 PMMark6 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Peter,

    In the tomcat /conf/server.xml file set the connectionTimeout value (it is in milli seconds). Then restart tomcat and you should be all set. Please see the section I pasted in below.

     

    <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->

    <!-- The port and connectionTimeout was changed from the default. The connectionTimeout is set to 110 seconds to be greater than .Net 2.0 framework of 100 seconds for HTTP Keep Alives -->

    <Connector port="8000" maxHttpHeaderSize="8192"

    maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

    enableLookups="false" redirectPort="8443" acceptCount="100"

    connectionTimeout="110000" disableUploadTimeout="true" />

     

    Enjoy.

     

  • Friday, May 25, 2007 8:16 AMPAsp Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks!

    /Peter

  • Monday, July 23, 2007 1:11 PMY2KPRABUMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    this is my solution in c# for the same problem using partial classes.

     

    public partial class HelloProxy2: HelloProxy

    {

    protected override System.Net.WebRequest GetWebRequest(Uri uri)

    {

     

    System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

    webRequest.SendChunked = true;

    webRequest.Timeout = 200000; // this should be greater than IIS / web server's timeout

    webRequest.ProtocolVersion = System.Net.HttpVersion.Version11;

    return webRequest;

    }

    }

     

     

    when initializing use the new Helloproxy2.

    private Helloproxy2 helloProxy = new Helloproxy2();

     

    thats it all set , it solved my issues.

  • Thursday, July 26, 2007 12:31 AMeigenman Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     Y2KPRABU wrote:

    this is my solution in c# for the same problem using partial classes.

     

    public partial class HelloProxy2: HelloProxy

    {

    protected override System.Net.WebRequest GetWebRequest(Uri uri)

    {

     

    System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

    webRequest.SendChunked = true;

    webRequest.Timeout = 200000; // this should be greater than IIS / web server's timeout

    webRequest.ProtocolVersion = System.Net.HttpVersion.Version11;

    return webRequest;

    }

    }

     

     

    when initializing use the new Helloproxy2.

    private Helloproxy2 helloProxy = new Helloproxy2();

     

    thats it all set , it solved my issues.



    I don't understand how this can solve the issue.  You are setting .net's timeout higher than the remote server's timeout and so you will get broken threads because the remote server will time out out the connection before your timer runs out, thus causing the request canceled error message.  I would think you must have your .NET client connection timeout shorter than the remote server timeout to avoid getting the canceled request error.
  • Thursday, November 29, 2007 7:32 PMboston_ma Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello,

     

    I have the same problem with the System.Net.WebException: The request was aborted: The request was canceled

    and when I try one of this code below to see it helps to solve the timeout problem but get this error message saying GetWebRequest(System.Uri)': no suitable method found to override, how do I fix this to make it work and use it as a function call?

     


     

    protected override WebRequest GetWebRequest(Uri uri)
            {
                HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest(uri);

     
                webRequest.KeepAlive = false;
                webRequest.ProtocolVersion = HttpVersion.Version10;
                return webRequest;
            }
  • Friday, November 30, 2007 6:57 PMY2KPRABUMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    manuel

    the code should be inside the derived class, where the base class is your actual proxy.

    is this your case eg

    public partial class HelloProxy2: HelloProxy

    {

    }

    where HelloProxy is the actual proxy got from web refernce.

     

     

  • Friday, November 30, 2007 9:33 PMboston_ma Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Hi Y2KPRABU,

     

    Can you help me with this? I have the same timeout issue like others when trying to upload large file from the client side to the server and it fails under 2 minutes, In the web config, I set the execution timeout  for 10 mins and the size up to 500mb, but when I try to upload a file size 120mb, it fails because the set the System.Net.WebException: The request was aborted: The request was canceled. I read some article in here that they use this method in code and it helps them to solve the timeout issue, I try one of the code below in the derived class uploadservice but get an  Error 1 'uploadservice.uploadservice.GetWebRequest(System.Uri)': no suitable method found to override, Can you or someone show me how do I set this code right in the class uploadservice and called this override function in the code? Is it a default override  when it hits this class uploadservice page?  I'm not really sure how this overide function work? can you or someone explains more for me please, thanks a lot!

     

    namespace uploadservice

    {

    public class uploadservice : System.Web.Services.WebService

    {

     

        protected override System.Net.WebRequest GetWebRequest(Uri uri)

        {

          System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

          webRequest.SendChunked = true;

          webRequest.Timeout = 200000;  // this should be greater than IIS / web server's timeout

          webRequest.ProtocolVersion = System.Net.HttpVersion.Version11;

         return webRequest;

       }

     

    }

    }

  • Saturday, February 02, 2008 1:31 PMvsa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Do you know how to create the proxy classes. My VS doesn't create the Reference.cs file. maybe it's turned off or something dunno. Is it maybe possible to create the proxy classes or the Reference.cs by manually?