The underlying connection was closed... again?

Published Mon, Oct 6 2008 4:22 PM

First of all: this is my very first blog post, on my very first blog. I'll probably have a lot to learn about blogging, and I hope any readers (a.k.a. you) will give me constructive feedback which will help me improve my writing skillz. I'm currently not too happy about the layout (too white, and too orange) but I may fix that later. Though I've recently learned, that there are people who like it :)

 

So, the underlying connection was closed, again.

Those of us who have been consuming webservices from a .NET application, will probably have seen the error message "The Underlying connection was closed: An unexpected error occurred on a send." (or a receive). 

Search for the error on google, and you'll get like 35k+ results. Most of which are blog postings or forum threads. Of course you'll get some MSDN articles. And other misc pages showing just the error message. 

A real common solution to this problem is the following, well known, piece of code:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
 System.Net.HttpWebRequest webRequest = 
  (System.Net.HttpWebRequest) base.GetWebRequest(uri);
 webRequest.KeepAlive = false;
 return webRequest;
}

Other common solutions involve, for example, tweaking the IIS server, or updating the .NET Framework to have the latest patches.

 

So what's new? 

Recently, we had the same problem in our project. Naturaly the first thing we did was check the known solutions. Most of them were already applied because of previous problems. A few solutions were new, but didn't do the trick.

Finally, we decided to use a little tool named tcpTrace. Hooking it up between our client app and the webservice allowed us to see all the http traffic going back and forth. The last incoming http data before the underlying connection got closed turned out to contain the following message from the webserver: "maximum request length exceeded". 

Turns out, the default maximum request length of IIS is 2 megabytes. Our SOAP request was a bit larger. Adding the following line to the system.web section of the webservice's web.config fixed it all:

<httpRuntime maxRequestLength="10240"/>

Comments

# Ramon Smits said on Friday, October 17, 2008 11:49 AM

Please dont hardcode this but tweak it in your app.config.

 <system.net>

   <settings>

     <servicePointManager expect100Continue="false" />

   </settings>

 </system.net>

# MarkO said on Tuesday, November 04, 2008 1:09 PM

@Ramon:

As far as I know, expect100Continue is a different property than keepAlive.

Besides, setting a servicePointManager (static) property in the web.config affects all web service proxies. This may not be what you want.

# Wholesale swimwear + China swimwear manufacturer said on Monday, January 21, 2013 3:04 AM

If New Year comes, can summer be so far behind? Summer is the cool season for most women since they can wear bikini and sexy swimwear on seaside or swim pool. Take a glance on wholesale swimwear, you will find some suitable for your business. The good quality and fast worldwide shipping of Dear-Lover make it top China Swimwear Manufacturerand famous worldwide.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 4 and 3 and type the answer here: