asp.net - 远程主机关闭了连接。 错误代码是0x800704CD
每当发生异常时,我都会收到来自我网站的错误电子邮件。 我收到此错误:
远程主机关闭了连接。 错误代码是0x800704CD
而且不知道为什么。 我每天大约30。 我无法重现错误,因此无法追踪问题。
网站是在IIS7上运行的ASP.NET 2。
堆栈跟踪:
at
System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32
result, Boolean throwOnDisconnect) at
System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
at
System.Web.HttpResponse.Flush(Boolean
finalFlush) at
System.Web.HttpResponse.Flush() at
System.Web.HttpResponse.End() at
System.Web.UI.HttpResponseWrapper.System.Web.UI.IHttpResponse.End()
at
System.Web.UI.PageRequestManager.OnPageError(Object
sender, EventArgs e) at
System.Web.UI.TemplateControl.OnError(EventArgs
e) at
System.Web.UI.Page.HandleError(Exception
e) at
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) at
System.Web.UI.Page.ProcessRequest(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) at
System.Web.UI.Page.ProcessRequest() at
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext
context) at
System.Web.UI.Page.ProcessRequest(HttpContext
context) at
ASP.default_aspx.ProcessRequest(HttpContext
context) at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at
System.Web.HttpApplication.ExecuteStep(IExecutionStep
step, Boolean& completedSynchronously)
webnoob asked 2019-06-12T22:27:39Z
6个解决方案
53 votes
我总是得到这个。 这意味着用户开始下载文件,然后失败,或者他们取消了。
要重现异常,请尝试自己执行此操作 - 但是我不知道有任何方法可以阻止它(除了仅处理此特定异常)。
您需要根据应用确定最佳前进方式。
m.edmondson answered 2019-06-12T22:28:07Z
25 votes
正如m.edmondson所说,“远程主机关闭了连接。” 当用户或浏览器取消某些内容或网络连接丢失等时发生。但不一定是文件下载,只是对任何导致客户端响应的资源的任何请求。 基本上,错误意味着无法发送响应,因为服务器无法再与客户端(浏览器)通信。
您可以采取许多步骤来阻止它发生。 如果您使用Response.Write,Response.Flush在响应中手动发送内容,从Web服务/页面方法或类似方法返回数据,则应在发送响应之前考虑检查Response.IsClientConnected。 此外,如果响应可能需要很长时间,或者需要进行大量的服务器端处理,则应定期检查,直到response.end(如果调用)。 有关此属性的详细信息,请参阅以下内容
[http://msdn.microsoft.com/en-us/library/system.web.httpresponse.isclientconnected.aspx]
或者,我认为最有可能在您的情况下,错误是由框架内的东西引起的。 以下链接可能有用:
[http://blog.whitesites.com/fixing-The-remote-host-closed-the-connection-The-error-code-is-0x80070057__633882307305519259_blog.htm]
以下堆栈溢出帖子也可能是有意义的:
Response.OutputStream.Write中的“远程主机关闭了连接”
Sam Shiles answered 2019-06-12T22:29:14Z
9 votes
可以使用以下代码重现错误:
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();
}
现在以调试模式运行网站。 在写入输出流的循环中放置一个断点。 转到该操作方法,并在第一次迭代通过后关闭浏览器的选项卡。 点击F10继续循环。 在达到下一次迭代后,您将看到异常。 享受你的例外:-)
Yaroslav Yakovlev answered 2019-06-12T22:29:44Z
2 votes
当我从WebRequest动态读取数据并且从未关闭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;
}
cbillowes answered 2019-06-12T22:30:08Z
1 votes
我是在asp.net 2.0 iis7 Windows2008网站上得到的。 iis6上的相同代码工作正常。 它给我带来了一个问题,因为它弄乱了登录过程。 用户将登录并获得302到default.asxp,这将通过page_load,但不是预渲染,直到iis7将302返回到login.aspx而没有auth cookie。 我开始玩应用程序池设置,由于某种原因'启用32位应用程序'似乎已修复它。 不知道为什么,因为这个网站没有做任何需要任何32位驱动程序的特殊功能。 我们有一些网站仍然使用需要32位的Access,但不是像我这样的直接SQL网站。
Rich Wilson answered 2019-06-12T22:30:35Z
1 votes
我在我写的图像处理程序上也遇到了同样的错误。 我现场每天30次,流量很大,设法重现它。 当用户取消请求时(例如,关闭页面或他的互联网连接被中断),你得到这个,在我的情况下,在下一行中:
myContext.Response.OutputStream.Write(buffer, 0, bytesRead);
我想不出任何阻止它的方法,但也许你可以妥善处理这个问题。例如:
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..
}
Robert Benyi answered 2019-06-12T22:31:07Z