.NET 2.0 Simultaneous Web Service Calls give “The request was aborted: The request was canceled.” error


We’ve been having web requests just randomly fail with an error message of “The request was aborted: The request was canceled.” Simple testing would never produce this result yet real-world use and stress testing would indeed cause this problem. If you Google it and you’ll find many other people with this exact same problem. But finding the actual reason for the problem is a bit more difficult. Luckily somebody (birch9397) opened a ticket with Microsoft and shared it, which hit the nail dead-on at the Microsoft Forums. Here is a snippet that contains the beef of the source of the problem:

It turns out that when you start to make spin up your second set of threads to call the remote web service your ASP.NET application is waiting for a response from the web service but instead the web server resets the connection. Within the dump that I collected we see that there is an exception with the text of “The underlying connection was closed: A connection that was expected to be kept alive was closed by the server”. It appears that the code logic in .NET 2.0 is that when this occurs we will try to resend the web request, however because of the previous exception the m_Aborted property of the HttpWebRequest object is set to true and so we end up getting the Request Canceled exception that you are seeing.

For our next troubleshooting steps I’d like for you to disable Http KeepAlives for the HttpWebRequest object in your .NET 2.0 application. This will force the client to establish another TCP session with the BEA Web server before sending out another web service requests.

In order to set the KeepAlive property of the HttpWebRequest object to false you will need to override the GetWebRequest method within your client application. Please see Resolution D in the following article to see how to do this in your ASPX client page

915599 You receive one or more error messages when you try to make an HTTP request in an application that is built on the .NET Framework 1.1 Service Pack 1

http://support.microsoft.com/default.aspx?scid=kb;EN-US;915599

Luckily, a user there named Antzone gives a simple code snippet showing you how you can resolve this issue for yourself. If you create a wrapper class around your base WebRequest class, you can override the GetWebRequest(Uri) method and set the HttpWebRequest.KeepAlive propert to false in just a couple lines of code. Then everywhere you instantiate your webrequest, simply instantiate this new web request instead.

Not bad and makes many errors go away! :)

-Jax

3 Comments

  1. Comment by Barry on December 16, 2008 1:25 pm

    But this will reduce the performance of your site.

  2. Comment by Pradeep on December 22, 2008 4:58 am

    Hi All,

    Thanks to this posting. I resolved the issue by writing the following code. Please note that I didn’t make use of keepAlive property as suggested in this post though it may be useful in some other condition.

    protected override WebRequest GetWebRequest(Uri uri)
    {
    HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest(uri);
    //Setting KeepAlive to false
    //webRequest.KeepAlive = false;
    //webRequest.ProtocolVersion = HttpVersion.Version10;

    ServicePointManager.DefaultConnectionLimit = 100;
    ServicePointManager.MaxServicePointIdleTime = 2000;

    return webRequest;

    }

  3. Comment by venkatx5 on July 18, 2011 8:17 am

    Thanks a lot Pradeep. The code you given is working for me.

Comments RSS TrackBack Identifier URI

Leave a comment


Jaxidian Update is proudly powered by WordPress and themed by Mukkamu