远程主机关闭了连接。错误代码是0x800704CD

无论何时发生异常,我都会收到来自我网站的错误邮件。我得到了这个错误:

远程主机关闭了连接。错误代码是0x800704CD

不知道为什么。我一天大概有30个。我也无法重现这个错误,因此无法跟踪问题。

网站是运行在 IIS7上的 ASP.NET 2。

堆栈跟踪:

在 System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError (Int32 结果,布尔值为 throwOnDisconnect) System.Web.Hosting.IIS7WorkerRequest 在 HttpResponse. Flush (Boolean 同花顺) System.Web.HttpResponse. Flush () at System.Web.HttpResponse. End () at System.Web.UI.HttpResponseWrapper. System.Web.UI.IHttpResponse. End () 在 System.Web.UI.PageRequestManager.OnPageError (Object 发件人,EventArgs e)在 System.Web.UI.TemplateControl.OnError (EventArgs E)在 System.Web.UI.Page. HandleError (异常) E)在 System.Web.UI.Page. ProcessRequestMain (Boolean 包括 AsyncPoint,Boolean 之前的阶段 包括 StagesAfterAsyncPoint) System.Web.UI.Page. ProcessRequest (Boolean 包括 AsyncPoint,Boolean 之前的阶段 包括 StagesAfterAsyncPoint) System.Web.UI.Page. ProcessRequest () at System.Web.UI.Page.ProcessRequestWithNoAssert (HttpContext 上下文) System.Web.UI.Page.ProcessRequest (HttpContext 上下文) Default _ aspx. ProcessRequest (HttpContext 上下文) System.Web.HttpApplication.CallHandlerExectionStep.System.Web.HttpApplication.IExectionStep.Execute () 在 System.Web.HttpApplication.ExecuteStep (IExectionStep 步骤、布尔值及同步完成)

147364 次浏览

I get this one all the time. It means that the user started to download a file, and then it either failed, or they cancelled it.

To reproduce the exception try do this yourself - however I'm unaware of any ways to prevent it (except for handling this specific exception only).

You need to decide what the best way forward is depending on your app.

I was getting this on an asp.net 2.0 iis7 Windows2008 site. Same code on iis6 worked fine. It was causing an issue for me because it was messing up the login process. User would login and get a 302 to default.asxp, which would get through page_load, but not as far as pre-render before iis7 would send a 302 back to login.aspx without the auth cookie. I started playing with app pool settings, and for some reason 'enable 32 bit applications' seems to have fixed it. No idea why, since this site isn't doing anything special that should require any 32 bit drivers. We have some sites that still use Access that require 32bit, but not our straight SQL sites like this one.

As m.edmondson mentioned, "The remote host closed the connection." occurs when a user or browser cancels something, or the network connection drops etc. It doesn't necessarily have to be a file download however, just any request for any resource that results in a response to the client. Basically the error means that the response could not be sent because the server can no longer talk to the client(browser).

There are a number of steps that you can take in order to stop it happening. If you are manually sending something in the response with a Response.Write, Response.Flush, returning data from a web servivce/page method or something similar, then you should consider checking Response.IsClientConnected before sending the response. Also, if the response is likely to take a long time or a lot of server-side processing is required, you should check this periodically until the response.end if called. See the following for details on this property:

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.isclientconnected.aspx

Alternatively, which I believe is most likely in your case, the error is being caused by something inside the framework. The following link may by of use:

http://blog.whitesites.com/fixing-The-remote-host-closed-the-connection-The-error-code-is-0x80070057__633882307305519259_blog.htm

The following stack-overflow post might also be of interest:

"The remote host closed the connection" in Response.OutputStream.Write

I got this error when I dynamically read data from a WebRequest and never closed the Response.

    protected System.IO.Stream GetStream(string url)
{
try
{
System.IO.Stream stream = null;
var request = System.Net.WebRequest.Create(url);
var response = request.GetResponse();


if (response != null) {
stream = response.GetResponseStream();


// I never closed the response thus resulting in the error
response.Close();
}
response = null;
request = null;


return stream;
}
catch (Exception) { }
return null;
}

I too got this same error on my image handler that I wrote. I got it like 30 times a day on site with heavy traffic, managed to reproduce it also. You get this when a user cancels the request (closes the page or his internet connection is interrupted for example), in my case in the following row:

myContext.Response.OutputStream.Write(buffer, 0, bytesRead);

I can’t think of any way to prevent it but maybe you can properly handle this. Ex:

        try
{
…
myContext.Response.OutputStream.Write(buffer, 0, bytesRead);
…
}catch (HttpException ex)
{
if (ex.Message.StartsWith("The remote host closed the connection."))
;//do nothing
else
//handle other errors
}
catch (Exception e)
{
//handle other errors
}
finally
{//close streams etc..
}

One can reproduce the error with the code below:

public ActionResult ClosingTheConnectionAction(){
try
{
//we need to set buffer to false to
//make sure data is written in chunks
Response.Buffer = false;
var someText = "Some text here to make things happen ;-)";
var content = GetBytes( someText );


for(var i=0; i < 100; i++)
{
Response.OutputStream.Write(content, 0, content.Length);
}


return View();
}
catch(HttpException hex)
{
if (hex.Message.StartsWith("The remote host closed the connection. The error code is 0x800704CD."))
{
//react on remote host closed the connection exception.
var msg = hex.Message;
}
}
catch(Exception somethingElseHappened)
{
//handle it with some other code
}


return View();
}

Now run the website in debug mode. Put a breakpoint in the loop that writes to the output stream. Go to that action method and after the first iteration passed close the tab of the browser. Hit F10 to continue the loop. After it hit the next iteration you will see the exception. Enjoy your exception :-)