繁体   English   中英

对 Stream 大文件使用 HttpWebRequest 时出现 Memory 异常

[英]Out of Memory Exception When Using HttpWebRequest to Stream Large File

使用大文件的 Http.Put 时出现 Out of Memory 异常。 如代码所示,我正在使用异步 model。 我正在尝试将 8K 数据块发送到 Windows 2008 R2 服务器。 当我尝试写入超过 536,868,864 字节的数据块时,始终会发生异常。 异常发生在下面代码片段中的 requestStream.Write 方法上。

寻找原因?

注意:较小的文件可以放置。 如果我写入本地 FileStream,逻辑也可以工作。 在 Win 7 Ultimate 客户端计算机上运行 VS 2010、.Net 4.0。

   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("Http://website/FileServer/filename");
   request.Method = WebRequestMethods.Http.Put;
   request.SendChunked = true;
   request.AllowWriteStreamBuffering = true;
   ...

   request.BeginGetRequestStream( new AsyncCallback(EndGetStreamCallback), state);
   ...

   int chunk = 8192; // other values give same result
   ....

   private static void EndGetStreamCallback(IAsyncResult ar) {
        long limit = 0;
        long fileLength;
        HttpState state = (HttpState)ar.AsyncState;

        Stream requestStream = null;
        // End the asynchronous call to get the request stream.

        try {
            requestStream = state.Request.EndGetRequestStream(ar);
            // Copy the file contents to the request stream.

            FileStream stream = new FileStream(state.FileName, FileMode.Open, FileAccess.Read, FileShare.None, chunk, FileOptions.SequentialScan);

            BinaryReader binReader = new BinaryReader(stream);
            fileLength = stream.Length;

            // Set Position to the beginning of the stream.
            binReader.BaseStream.Position = 0;

            byte[] fileContents = new byte[chunk];

            // Read File from Buffer 
            while (limit < fileLength)
            {
                fileContents = binReader.ReadBytes(chunk);

                // the next 2 lines attempt to write to network and server
                requestStream.Write(fileContents, 0, chunk);   // causes Out of memory after 536,868,864 bytes
                requestStream.Flush();  // I get same result with or without Flush

                limit += chunk;
            }

            // IMPORTANT: Close the request stream before sending the request.
            stream.Close();

            requestStream.Close();
        }
    }

您显然有这个记录在案的问题 AllowWriteStreamBufferingtrue时,它会缓冲写入请求的所有数据,因此,“解决方案”是将该属性设置为false

要解决此问题,请将 HttpWebRequest.AllowWriteStreamBuffering 属性设置为 false。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM