C# Response.OutputStream.Write 中的“远程主机关闭了连接”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/831447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:24:13  来源:igfitidea点击:

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

c#asp.netstreaming

提问by spaetzel

This code streams large files to our users:

此代码将大文件流式传输给我们的用户:

                // Open the file.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read, System.IO.FileShare.Read);


            // Total bytes to read:
            dataToRead = iStream.Length;

            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Response.IsClientConnected)
                {
                    // Read the data in buffer.
                    length = iStream.Read(buffer, 0, 10000);

                    // Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length);

                    // Flush the data to the HTML output.
                    Response.Flush();

                    buffer = new Byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }

Every once and a while we recieve this exception:

每隔一段时间我们就会收到这个异常:

The remote host closed the connection. The error code is 0x80072746

Here is the full stack trace:

这是完整的堆栈跟踪:

Stack Trace:
   at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async)
   at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)
   at System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush)
   at System.Web.HttpResponse.Flush(Boolean finalFlush)
   at System.Web.HttpResponse.Flush()
   at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)
   at System.Web.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at BIS.DocumentBus.Controls.DocumentViewer.StreamFile(String filepath)

We have never had evidence that users are having trouble downloading our files and plan to simply ignore this exception.

我们从未有证据表明用户在下载我们的文件时遇到问题并打算简单地忽略此异常。

Any idea what the source of this problem is? Is it safe to ignore?

知道这个问题的根源是什么吗?忽略是否​​安全?

采纳答案by David

That exception means that the client downloading the file broke the connection before the file had finished downloading. i.e. The client navigated to another page, or just closed the browser.

该异常意味着下载文件的客户端在文件下载完成之前断开了连接。即客户端导航到另一个页面,或者只是关闭了浏览器。

I might try moving the if (Response.IsClientConnected)line after your iStream.Read. Even if you did that, I think there still might be a chance to receive this error if the connection is broken while the OutputStream.Writemethod is still working.

我可能会尝试if (Response.IsClientConnected)在您的iStream.Read. 即使您这样做了,我认为如果在该OutputStream.Write方法仍在工作时连接断开,仍有可能收到此错误。

回答by Jon Hanna

There are a few different possible causes of this. I can think of three:

这有几个不同的可能原因。我能想到三个:

One is filling the buffer with close to 2GB of data, but this shouldn't be the case here, since you are flushing regularly.

一种是用接近 2GB 的数据填充缓冲区,但这里不应该是这种情况,因为您要定期刷新。

Another is indeed that described in the answer you previously accepted. It's very hard to reproduce, so I wouldn't assume it was necessarily wrong.

另一个确实是您之前接受的答案中描述的。很难复制,所以我不会认为它一定是错误的。

Another possible case, and the one I would bet on, is that the executionTimeout is exceeded, which would cause a ThreadAbortException at first, but this could in turn cause the failure of Flush() which would turn into the exception noted

另一种可能的情况,也是我敢打赌的情况,是超过了 executionTimeout,这首先会导致 ThreadAbortException,但这反过来可能会导致 Flush() 失败,这会变成所指出的异常

回答by Tomas Voracek

Increase executionTimeout in httpRuntime element of web.config.

增加 web.config 的 httpRuntime 元素中的 executionTimeout。

If user is downloading large file on slow connection, request will eventually timeout.

如果用户在慢速连接上下载大文件,请求最终会超时。

回答by immayankmodi

I am posting this answer because it might be help others and save some important time.

我发布这个答案是因为它可能会帮助其他人并节省一些重要的时间。

In my case Response.Buffer = truein download method (on very first statement) solved the issue.

在我的情况下Response.Buffer = true,下载方法(在第一个声明中)解决了这个问题。

Thanks

谢谢