The underlying connection was closed... again?
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 =
webRequest.KeepAlive = false;
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: