I get a memory exception when using the Http.Put large file. I am using an asynchronous model as shown in the code. I am trying to send 8K data blocks to a Windows 2008 R2 server. An exception occurs when I try to write a data block that exceeds 536,868,864 bytes. An exception occurs in the requestStream.Write method in the code snippet below.
Looking for a reason?
Note. Smaller PUT OK files. The logic also works if I write to a local FileStream. Running VS 2010, .Net 4.0 on a 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(); } }
pearcewg
source share