Search code examples
c#.netpostwindows-phone

.Net HTTP POST FileStream / without using MemoryStream


HttpClient.PostAsync(uri, StreamContent) seems to use a MemoryStream behind the scenes even if I create StreamContent using a FileStream. Is there any way to HTTP POST a FileStream from .Net without using a MemoryStream? Can I modify MS.Internal.InternalWebRequestStream.Write (see stack trace below) to not use a MemoryStream?

Stack trace:

Exception of type 'System.OutOfMemoryException' was thrown. 

   at System.IO.MemoryStream.set_Capacity(Int32 value) 
   at System.IO.MemoryStream.EnsureCapacity(Int32 value) 
   at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) 
   at MS.Internal.InternalWebRequestStream.Write(Byte[] buffer, Int32 offset, Int32 count) 
   at System.IO.Stream.<BeginWriteInternal>b__11(Object param0) 
   at System.Threading.Tasks.Task`1.InnerInvoke() 
   at System.Threading.Tasks.Task.Execute() 

Solution

  • The solution is to use an HttpWebRequest and set AllowWriteStreamBuffering = false as described here.