The underlying connection was closed,让我很崩溃!!

以下摘自:

http://bloggingabout.net/blogs/marko/archive/2008/10/06/the-underlying-connection-was-closed-revisited.aspx

拱学习之用,同时也希望能解决这个问题.

 

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"/>

你可能感兴趣的:(Connection)