内存不够的异常使用的HttpWebRequest当到流大文件 [英] Out of Memory Exception When Using HttpWebRequest to Stream Large File
问题描述
我用一个大文件的Http.Put时得到一个内存溢出异常。我使用异步模型中所示的代码。我想数据的8K块发送到Windows 2008 R2服务器。当我尝试写超过536868864字节的数据块的异常持续发生。在下面的代码片段代码中的requestStream.Write方法发生异常。
寻找原因?
请注意:较小的文件都将确定。如果我写一个本地的FileStream逻辑也适用。在Win 7旗舰版客户端的计算机上运行VS 2010,.NET 4.0。
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(宀://网站/文件服务器/文件名);
request.Method = WebRequestMethods.Http.Put;
request.SendChunked = TRUE;
request.AllowWriteStreamBuffering = TRUE;
...
request.BeginGetRequestStream(新的AsyncCallback(EndGetStreamCallback),状态);
...
INT块= 8192; //其它值给出相同的结果
....
私有静态无效EndGetStreamCallback(IAsyncResult的AR){
长限制= 0;
长文件长度;
的HttpState状态=(的HttpState)ar.AsyncState;
流requestStream = NULL;
//结束异步调用获得请求的流。
尝试{
requestStream = state.Request.EndGetRequestStream(AR);
//文件内容复制到请求流。
的FileStream流=新的FileStream(state.FileName,FileMode.Open,FileAccess.Read,FileShare.None,块,FileOptions.SequentialScan);
BinaryReader binReader =新BinaryReader(流);
=文件长度stream.Length;
//设置位置到流的开头。
binReader.BaseStream.Position = 0;
字节[] = fileContents新的字节[块]
//从缓冲区
读取文件时(限<文件长度)
{
fileContents = binReader.ReadBytes(块);
//下面两行尝试写入网络和服务器
requestStream.Write(fileContents,0,块); //导致内存耗尽之后536868864字节
requestStream.Flush(); //我得到有或没有冲洗
+限块=相同的结果;
}
//重要提示:在发送请求之前关闭请求流。
stream.Close();
requestStream.Close();
}
}
您显然有这个记录问题。当 AllowWriteStreamBuffering
是真正
,它缓冲的所有数据写入请求的!因此,解是该属性设置为假
:
要解决此问题,将HttpWebRequest.AllowWriteStreamBuffering属性设置为false。
块引用>I get an Out of Memory Exception when using Http.Put of a large file. I am using an asynchronous model as shown in the code. I am trying to send 8K blocks of data to a Windows 2008 R2 server. The exception consistently occurs when I attempt to write a block of data that exceeds 536,868,864 bytes. The exception occurs on the requestStream.Write method in the code snippet below.
Looking for reasons why?
Note: Smaller files are PUT OK. Logic also works if I write to a local FileStream. Running VS 2010, .Net 4.0 on Win 7 Ultimate client computer.
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(); } }
解决方案You apparently have this documented problem. When
AllowWriteStreamBuffering
istrue
, it buffers all the data written to the request! So, the "solution" is to set that property tofalse
:To work around this issue, set the HttpWebRequest.AllowWriteStreamBuffering property to false.
这篇关于内存不够的异常使用的HttpWebRequest当到流大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!