"远程主机关闭了连接"在Response.OutputStream.Write [英] "The remote host closed the connection" in Response.OutputStream.Write

查看:844
本文介绍了"远程主机关闭了连接"在Response.OutputStream.Write的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code流大文件给我们的用户:

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?

推荐答案

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

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.

我可能会尝试后,你的 iStream.Read 移动如果(Response.IsClientConnected)行。即使你这样做,我认为仍有可能收到此错误如果在 OutputStream.Write 的方法仍然是工作的连接断开的机会。

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.Write method is still working.

这篇关于"远程主机关闭了连接"在Response.OutputStream.Write的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆