Geeks With Blogs
Denis Pitcher Denis Pitcher's Tech Blog

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.

I've been trying to figure out what the cause of this problem is for a few days now and I've finally tracked down the error.

It's due to a bug in .net that closes the connection prior to it being finished.  I'm concerned about the performance issues involved in reconnecting, however I have written my code to keep most connections and data transfer small anyways, I'll have to keep an eye on the performance of this crappy bug.

A good reference for solving this issue is available at: http://p2p.wrox.com/topic.asp?TOPIC_ID=4858

Unfortunately the microsoft KB article that addressed the issue is no longer available.

As suggested I have added the following code to my reference.cs file (which needs to be done each time I update the webservice reference) to assign the keepalive value to false to allow the connection to be closed and reopened.

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

            webRequest.KeepAlive = false;
            webRequest.ProtocolVersion=HttpVersion.Version10;
            return webRequest;
        }

I have also added a reference to System.Net via a using statement to import the HttpWebRequest namespace.


Posted on Tuesday, August 16, 2005 2:31 PM | Back to top


Comments on this post: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I have encountered this issue in our web application as well and we are currently testing this solution for improved stability. In similar posts I have seen a discrepancy between the code snippets: some indicate the need to specify HTTP version 1.0, others omit that statement. Is it necessary? Does the solution not work if we leave the default of HTTP 1.1?
Left by Ben Harper on Sep 11, 2005 12:31 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
From the research I've done, the issue is related to a bug in .Net that causes connections to be lost during a .net webservice call.

Essentially requests are made by opening a connection, making a number of them then closing it. In this occurance, the connection times out but is not recognized as closed and thus a new connection is not established for the next request and an error is returned.

Setting the keepalive to false sets it to close and reopen connections for each request. This taxes the server quite a bit more (some 7 connections for each new request). I would welcome any solutions to this error that don't involve this much traffic but I have yet to find any and am reluctant to write around it.

The suggestions I found indicated that setting the keepalive should solve it, yet some suggested setting the http version to 1.0.

There are variations in how the 1.0 and 1.1 versions of how the protocol work. Officially, the version 1.0 doesn't support keep-alives, however some implementations do support it.

.Net is designed to use http 1.1 to make requests which encourages persistant connections. It is more likely you'll avoid the error by forcing the use of the 1.0 protocol as it didn't take advantage of persistant connections (multiple requests per connection), however I cannot say either way as to whether the error won't still occur with the version set as 1.1

For example, leaving it set to use the 1.1 protocol will likely not try to keep the connection alive, but may still try to send more requests per connection which may cause the error to happen, just less frequently.

Unfortunately I cannot track down the original microsoft documentation on this issue as it seems to have disappeared, so outside of studying the documented http 1.0 and 1.1 versions of the protocol, comparing them and studying the .net implementation, there isn't much you can do aside from setting both to avoid the error.


Left by Denis Pitcher on Sep 13, 2005 12:10 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Thanks for the great article. We have also been experiencing the same error on our server. We have multiple web sites hosted in IIS on a 2003 server.

Our clients, specially a selected few, initially received the error with great frequency. But after a bit of research, we decided to turn off KeepAlives on all web sites right on the server. That made a tremendous difference. We are, however, still getting the same error with much lower frequency.

So we are now contemplating whether your proposed solution will make any difference, as the KeepAlives are already off in IIS. Unless we assume the HTTP version downgrade may be of some help.
Left by BobbyA on Sep 15, 2005 8:35 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Dennis,
You're awesome! I've been beating my head on this one for a while. Thank you for contributing this great info!

(good) Karma.upload(pitcher.dennis);
Left by CJ on Sep 24, 2005 1:09 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
How does this "fix" affect webservice calls using threading? I have a .net windows app that uploads binary data (files) in separate threads.
Left by Justin Rekas on Oct 03, 2005 12:47 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Justin,

Thats a good question that I am unsure of the answer. Of the threaded webservice I've written, I didn't encounter this issue because I was interacting with a propriatary webservice.

I would assume threading would simply dictate more calls to be made as better use of IO and connections would likely occur. This may reduce the likelyhood of this error occuring but would not stop the error.

Seeing as this error is tied to the connection being kept alive. If the connection is believed to be alive but is actually terminated (as in this bug), I believe you would still encounter the same error, threaded or not.

I would welcome some feedback on this as I havn't explored the issue much in regards to threads.
Left by Denis Pitcher on Oct 03, 2005 1:05 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
try:

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

Thread.Sleep(1 * 1000);

...
Left by KaosOverride on Nov 10, 2005 4:13 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I do have the same problem under a load-balanced environment.

We are using 2 F5 Big-Ip.

Left by Max on Nov 17, 2005 6:26 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I had this same issue in a windows application I wrote. The application polls remote IIS Servers on a regular cyclic basis hosting an ASP page which packages up data and delivers it to the app for download. The application must use NT Authentication to auth with the ASP page and then performs an HTTP post followed by a download over SSL. The application sits on a windows 2000 server in the live environment and I used to see this error intermittently, but usually after the first few successful download cycles.

I tried the work around involving disabling keep-alives but this prevented NT Authentication from working so clearly wasn't an option. However after some testing I have managed to eradicate the fault by uninstalling .Net framework 1.1 and leaving only .Net framework version 2. The issue I am left with is on my development machine which requires .Net framework 1.1 to run VB.Net 2003. This also isn't an ideal solution since future upgrades/OS rebuilds when I have left the company must ensure .Net Framework ONLY is installed. That said after successfully running this without one error in just under a month ... it makes my head feel a whole lot better now the bruises from the table have cleared up!

Hope this helps anyway guys and if anyone else spies a more reliable, permanent fix short of upgrading to Visual Studio 2005 I'd be very grateful to hear of it!

Dave
Left by Dave on Jan 04, 2006 8:32 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I am getting the same error but in my case it seems to follow a pattern. The first time I make a call I get this error. If I try it the second time though, the call goes through. Is this the same issue, I mean the Keep Alive being set to true by default?
Left by Aditya on Feb 07, 2006 6:57 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I am using .net remoting to call an ejb using Ja.Net interoperability tool and i am using HTTP over SOAP channel. I get the same error message when i try to create a JNDI context.
How can i apply the "KeepAlive" in this case ?

My remoting client code is as follows :

RemotingConfiguration.Configure(@"D:\Sample\J-Integra\XMLDataTransfer\remoting_tcp.config");
Client client = new Client();
JNDIContext context = new JNDIContext();
client.home = (EchoHome)context.Lookup(HOME,JNDI);
client.remote = client.home.create();
Left by Gurudath S Gaddad on Jun 09, 2006 4:59 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Please see the knowledge base article recently published here: http://support.microsoft.com/default.aspx?scid=kb;EN-US;915599

This covers many known resolutions to the underlying connection closed exceptions
Left by Mike Flasko on Jun 15, 2006 9:26 PM

# Cool in .NET 2.0: HttpListener
Requesting Gravatar...
For the past week, I've been busily converting a .NET 1.1/VS 2003 solution with about 350,000 lines of...
Left by Matt's Weblog on Jul 31, 2006 12:38 PM

# Worked like a charm
Requesting Gravatar...
I was getting a the following exception when the System.Net.HttpWebResponse.GetResponse method was invoked (.NET 2.0; worked perfect in .NET 1.1):

"The underlying connection was closed A connection that was expected to be kept alive was closed by the server"

The exception was sporatic. For some odd reasons, it only failed every other time.

Setting the KeepAlive and ProtocolVersion properties of the HttpWebRequest instance solved the issue.

System.Net.HttpWebRequest httpRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(requestUri.ToString());

httpRequest.KeepAlive = false;
httpRequest.ProtocolVersion = HttpVersion.Version10;
Left by Douglas Leung on Aug 24, 2006 12:30 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Can someone please post a VB.NET version to the above solution?
Left by yaron on Oct 16, 2006 9:57 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Imports System.Net


Protected Overrides Function GetWebRequest(ByVal uri As Uri) As WebRequest
Dim webRequest As HttpWebRequest = CType(MyBase.GetWebRequest(uri), HttpWebRequest)
webRequest.KeepAlive = False
webRequest.ProtocolVersion = HttpVersion.Version10
Return webRequest
End Function
Left by Anon Programmer on Oct 17, 2006 6:06 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Hi All,

This is bug in IIS 6.0. To resolve this issue just install windows service pack 1 on thewindows 2003 server
Left by Vijay Kumar on Dec 22, 2006 7:11 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I am running my web services on Winodws Server 2003 w/ Service Pack 1 and have the same issue. Oddly I have only one user with this issue. When he runs the client on his home network (Hughes) he has the problem, on any other network he does not have a problem. This does not indicate to me that it is a bug in IIS, but I am still stumped.
Left by plclogic on Feb 08, 2007 7:11 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Hi!
I had the same problem with only one client... The problem was a misconfiguration of the IE proxy settings... We fixed it and voila!! all running ok now... Wish this helps you...

Regards,

Daniel
Left by Daniel Vanzo on Feb 09, 2007 5:59 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
We're seeing this extremely rare bug on a few customers' machines. I've tried KeepAlive=false with no luck. Now we'll try HTTP1.0 as well to see if that makes any difference....
Thanks for the helpful page.
Left by Taytay on Mar 20, 2007 9:54 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I had this problem. Here's my fix:

webRequest.UsePassive = false

Left by Mitch Lake on Mar 28, 2007 6:00 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I have this problem only if the previous call to the web service has a time out.

Overriding WebRequest GetWebRequest(Uri uri) does not solve my problem.

Any idea?
Thanks, regards Andrea
Left by andrea manara on Apr 13, 2007 3:37 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
HI,

For me it was the proxy, I disabled it totaly and everything
works!!

thanks a lot
Left by David on May 08, 2007 4:28 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I was having the exact problem listed here.

Reapplying windows server 2003 service pack 1 fixed the issue for me. Even though the service pack already existed on the machine
Left by Mitch on Jun 11, 2007 10:30 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Upgraded to new servers recently and have encountered this problem - causing our web application to crash.
Our network guys installed service pack 2 on this new server. Anyone else having a problem with sp2? I'm considering going back to sp1.
Left by Joe on Aug 30, 2007 2:07 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Thanks for the fix. I tried the KeepAlive = false fix and the error still does occur but after a lot more rows are processed. I can process 1619 rows before I encounter an error. This means that the as the number of connections to the server are made, it take longer and longer for the web service operation to complete because the server is under increasing load. It also has the unfortunate side-effect of filling up the database transaction log, requiring a manual flush.

Now I will try setting the proxy timeout to System.Threading.Timeout.Infinite and see if it can complete successfully.
Left by Shailen Sukul on Sep 18, 2007 7:28 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
ok it broke again with the exception: The underlying connection was closed: An unexpected error occurred on a receive. --> System.IO.IOException Unable to read data from the transport connection ...

This time 1694 rows were processed before the error occurred.
Left by Shailen Sukul on Sep 18, 2007 4:58 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I noticed that this error ("The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.") started to appear after we migrated our codebase to 2.0. I tried the KeepAlive=false solution but that did not work on its own consistently. I found that if you assign a random unique value to the ConnectionGroupName property that it works like a charm. Here is my override...


protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
//throw new Exception("Custom WebRequest override code hit!!");
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
webRequest.KeepAlive = false;
webRequest.ConnectionGroupName = Guid.NewGuid().ToString();
return webRequest;
}


Works like a charm! You can uncomment the exception throw to test that the override is in fact getting executed. Just paste this into the reference.cs and you're ready to go. Hope that helps...

Left by Debbie on Sep 26, 2007 1:22 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Can you please direct me where should i add the override funciton on the preference.cs file.

I tried open the file and dont know where to add those lines?

Thanks
E
Left by eyal on Nov 05, 2007 9:11 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Check that this is not caused by a recursive function loop

public void vSomeFunction(int i string s){

vSomeFunction(1,"Hello");

}

This can crash the application pool
Left by VAInternet on Dec 03, 2007 2:08 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
MS KB Article (Resolution D):
http://support.microsoft.com/kb/915599/en-us

Left by hB on Dec 09, 2007 1:07 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I have developed a console based application that extracts data from xml. This code worked properly unitl last time i excuted the code for processing large number of xml files.

I am having the same error you guys discussing above but mine is console application. Did the tracing its throwing exceptinon while i try to load the XML. i.e

XmlDocument xdoc = new XmlDocument();
xdoc.Load(fs);


by the i am using NET 1.1 and VS 2003. Any suggestion for me?



Left by Ahmedur Rab on Feb 06, 2008 1:29 PM

# Try <httpRuntime maxRequestLength="1048576" />
Requesting Gravatar...
I had the same problem. Non of the solutions worked for me. I Checked the error on server side in Global.asax. Then I realized I had to add
<httpRuntime maxRequestLength="1048576" />
to the web.config or the machine.config
Hope It works for you too
Left by Amir Sharif on May 21, 2008 12:27 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
The issue we have is this: IIS apps on a DMZ server call a web service on the internal network to process logins. Whenever the machine hosting the webservice is rebooted, the IIS machine starts throwing this error. The only way to resolve it is to do IISReset on the DMZ machine. I have KeepAlives off and 1.0 enabled in the call to the web service from the DMZ code. Seems like it has to be a caching problem somewhere. I tried touching the web.config of the problem application but that didn't 'reset' it. I have added some of the suggestions above and will see if it has any effect. Namely:
System.Net.HttpWebRequest Req = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
Req.Timeout = -1;
Req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
Req.ConnectionGroupName = Guid.NewGuid().ToString();
Req.KeepAlive = false;
Req.ProtocolVersion = HttpVersion.Version10;
return (WebRequest)Req;

We'll see what happens. Thoughts?
Left by Wade on May 23, 2008 12:07 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Well, those changes didn't help. This time, when our DMZ server (Windows Server 2003 Std. SP 1) started throwing the error, I went to my dev server and fired up an app that calls the same web service. That machine could hit the service, no problem. So...I know the service is up and responding to requests. And I can manually hit the service from the DMZ by using a browser so I know the DMZ machine can actually get to it. It's just something inside of the IIS process(es) that is flaking out. I haven't upgraded the windows server to SP 2 yet. Maybe that would help. Does anyone think that the part of the error text 'an unexpected error ...on RECEIVE' is significant?
Left by Wade on May 23, 2008 11:18 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
OK, more info. It apparently has nothing to do with the rebooting of the machine that has the service on it. The error occurrs just randomly. I patched Windows Svr 2003 up to SP 2. We'll see if that makes a difference. My next solution is to do a call to the web service w/o using the WebRequest object. If that fails, I may make an unmanaged call via COM to the dang thing!
Left by Wade on May 24, 2008 10:55 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
So far, after patching to SP2, we have had no problems. It's only been about 3 days but that is still significant!
Left by Wade on May 30, 2008 12:13 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
i created one console aplication -(,net 2.0)for uploading file from my local system to one ftp site -(,net 2.0)
for this i am using the ftp web request(file uploader concept)
i am getting the same error --
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
--
can any body help me
Left by nob on Jun 30, 2008 10:12 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I found out that the SP2 patch to our server never happened so it could be that for me. I doubt it, though. I suspect that it is just a pure bug in IIS whereby an http connection is trying to get re-used or something even though it shouldn't because after restarting IIS, it may run fine for a week or two.

Nob, have you tried using your same code to upload to a different site or does it fail for all of them?
Left by Wade on Jul 01, 2008 9:38 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I am calling a web service from a windows application.Everyting runs fine in my development and SIT server.But the same code bas throws the following error in my UAT server: "System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing"

The difference between SIT and UAT server is
1) UAT server is load balanaced but SIT is not
2) in SIT the web service is accessed by HTTP but in UAT it is accessed by HTTPS.

None of the resolution in this article has resolved my issue. Any help would be greatly appreciated.

Thanks in advance.Tanweer
Left by Tanweer on Jul 29, 2008 3:13 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
my 2 cents:

req.Method = "Get" was causing the error for some of my sites, but not others. commented this line out solved my problem.
Left by Johnny on Jul 30, 2008 7:22 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
We have this problem also.
It seems to only affect certain web services. Others work normally.

The only solution I have that works is to completely delete the entire database and then recreate a new database and repopulate. I make no changes to configuration or anything else. And then it magically works again.
So, possibly there is some connection to the database that is stuck open that is closed when the database is deleted.
Left by Mark David on Oct 09, 2008 4:00 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
hi.
i want to try to set keepalive to false. but i m not sure if i could successfully set it or not.
i created a new class and in that class i override the GetWebRequest function. in the function body, i putted a line as follows:

throw new Exception("OKOK");

so i would understand if i set it or not. but that doesnt throw me OKOK exception message. does this mean that i couldnt override the function properly?
Left by fer on Oct 20, 2008 6:37 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
None of the resolution fixed my issue..

here is my code:
//Initialisation
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", strPage, strVars));
//This time, our method is GET.
Thread.Sleep(1 * 1000);

WebReq.Method = "GET";
WebReq.Timeout = System.Threading.Timeout.Infinite;
WebReq.Credentials = CredentialCache.DefaultCredentials;
WebReq.ProtocolVersion = HttpVersion.Version10;
WebReq.KeepAlive = false;
WebReq.ConnectionGroupName = Guid.NewGuid().ToString();


//From here on, it's all the same as above.
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);

//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());

This is a console application calling http://digg.com.. but no reply..I tried to call the some other site like microsoft.com.. it works for me.. but not for digg.com.. Any answers why?
Left by Jag on Nov 18, 2008 8:53 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
To Daniel Vanzo:
I met this problem these days.And I found that
if Client's System Time is defferent from Server's System Time,a exception will be thrown.

"The underlying connection was closed: An unexpected error occurred on a receive."

Hope It works for you.
Left by Junjie Yao on Dec 24, 2008 4:52 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
For me, I accessed the IE on the server on which IIS is being hosted. As per its security, it popped up whether to keep the url in its secure zone, i added it and tried to synchronise the wsus again and that was it.
Left by Kwasi on Mar 04, 2009 12:56 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Please, this is very urgent.
i have a mobile application that access to a web service.
some times it returns the error: (i don’t know what to do)


windows mobile system.net.webexception: unable to read data from the data transport system.net.socket exception: an existing connection was forcibly
Left by Ricardo on Jun 17, 2009 4:23 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I also had this issue and was able to solve it. The error is very misleading. Afer thaving solved this I do not think it is a defect with a specific version of .net or with timeouts etc etc.

I had one web service with only one method and i was receiving this error anytime i called the method from a windows client. Because I only had 1 method in my web service this was fairly easy for me to troubleshoot. As I started pulling code out of this method to eliminate the problem I began receiving errors in other areas of my webservice during testing (run time errors, not compile time errors). It turns out I had made a mistake in a class that was being called by my web service. I had a public property that called itself over and over again like so.

public string MyProperty
{
get{return MyProperty}
}

instead of

public string MyProperty
{
get{return _myProperty}
}


Because a compile time exception is not created with the code above the code compiles without issue. However this coding problem caused a stack overflow to occur in my web service every time i called it and that caused the error at the top of this page. The error was very misleading and was all caused by a defect in my code.

Hope this helps somebody.
Left by Matt on Sep 01, 2009 8:46 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I was facing the same issue( System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.). After lot of troubleshooting I found that the somehow the host file under C:\WINDOWS\system32\drivers\etc has changed and was containing wrong IP Address for my site. After I corrected it to point to right one it started working.

Hope this helps!!!
Left by Pawan Gupta on Oct 21, 2009 4:43 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I had the same error a week ago and I found a solution here :

http://www.asp.net.nepalesemap.com/index.php?topic=12.0

There are several solutions for this. You can find the list of solutions here:

http://www.asp.net.nepalesemap.com/index.php?board=2.0
Left by cindy on Dec 21, 2009 6:30 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Probably you are getting block because the User-Agent is empty.
Set the User-Agent with something in the Request Header

That words for me

Example:

webClientRetriever.Headers.Add("User-Agent", "MyProject");
Left by Jkalderonkiros on Jan 05, 2010 10:38 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I am having similar problem. i am calling a method from a webservice and i keep getting the error intermittently. I have 2005 c# webservice running on Windows 2003 server SP1. i do not have a reference.cs file to change the keep alive settings. the web reference contains .disco and .map files. i am not sure where i can specify the keep alive to use false.

anything i have read up on this hasn't helped yet so i decided to upgrade to SP2...any suggestions?

Error:
System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
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)
Left by Ashwini on Feb 11, 2010 9:15 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I am also facing the same problem as Ashwini
someone kindly reply with a fix.

System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)

Thanks
Left by Rishi Gupta on Mar 02, 2010 5:51 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Hello,
I have the same problem but in the FTP upload

The underlying connection was closed: An unexpected error occurred on a receive.

My code is :

private FtpWebRequest CreateRequest(Uri serverUri, string action)
{
FtpWebRequest request = (FtpWebRequest) WebRequest.Create(serverUri);
//Stream throttledStream = new ThrottledStream(Response.OutputStream, 20);

request.Credentials = new NetworkCredential(this.UserName, this.Password);
request.UsePassive = true;
request.KeepAlive = false;
request.Method = action;
return request;
}

I can upload till 9,8 MB and after I lose the connection :)
Any Idea or help

Thanks
Houssam
Left by Houssam on Mar 22, 2010 6:17 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Hello,
I have the same problem on calling a web service

The underlying connection was closed: An unexpected error occurred on a receive.

I applied the fix below as recommended but when I override the web request, I now get this error: The operation has timed out

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

webRequest.KeepAlive = false;
webRequest.ProtocolVersion=HttpVersion.Version10;
return webRequest;
}

Please help

Thx

Ray
Left by Ray on Apr 30, 2010 1:27 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Thanks man! I have been fighting with problem for awhile...
Left by Andrey N on May 05, 2010 9:05 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
I had the same problem but it was not the KeepAlive that messed it up for me. After some testing i found out that I had to increase the maxItemsInObjectGraph property.

Add

<endpointBehaviors>
<behavior name="XXX">
<dataContractSerializer maxItemsInObjectGraph="1000000" />
</behavior>
</endpointBehaviors>

Then set your behaviour to the endpoint

<endpoint address="" binding="aaa" bindingConfiguration="aaa" name="aaa" bindingNamespace="aaa" contract="aaa" behaviorConfiguration="XXX"/>

I have also heard that problems like this may be caused by using enums in the data contracts. Might be worth looking at that if your problem remains.
Left by Skott on Jun 01, 2010 7:20 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
It works for me in a .NET 2.0 application usine Web Reference.
Really, this is a big workaround.

Thank you.
Left by Prodis on Jun 14, 2010 2:54 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Everyone,

I recommend you check out AskF5's explaination on this issue as it has fixed our "The underlying connection was closed: An unexpected error occurred on a receive." error. Basically the issue isn't .NET or keepalives or anything like that but a bug in IIS 6.0. IIS 6.0 by default sometimes sends an extra HTTP 100 Continue message which by the RFC2616 standard is incorrect. Microsoft has even issued a patch here: http://support.microsoft.com/kb/898708/en-us

The reason why disabling keepalives worked most of the time is it wasn't able to use this continue feature as much.

Patch up your IIS and keep your keep alives.

This is a real head scratcher when you look at it from the code point of view as your have coded it right. IIS 6.0 is causing pain, look to the patch in KB 898708.

-Joe
Left by joekiller on Feb 22, 2011 1:30 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Ask F5 article here: http://support.f5.com/kb/en-us/solutions/public/5000/900/sol5922.html

Doh!
Left by joekiller on Feb 22, 2011 1:31 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Your post help me resolve my issue. I am looking for this from last 4 days.

Thank you buddy!
Left by Devubha Manek on May 14, 2011 2:12 AM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
very nice article
Left by hello on Sep 04, 2011 11:49 PM

# re: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Requesting Gravatar...
Anyone have any suggestions for occasions where you are connecting to an oracle native webservice( 11g), meaning IIS is not the root cause?


Thanks
Left by Paul Eden on Oct 18, 2012 2:38 PM

Your comment:
 (will show your gravatar)


Copyright © Denis Pitcher | Powered by: GeeksWithBlogs.net | Join free