.Net HTTP POST FileStream / 不使用 MemoryStream
.Net HTTP POST FileStream / without using MemoryStream
HttpClient.PostAsync(uri, StreamContent)
似乎在幕后使用了 MemoryStream
,即使我使用 FileStream
创建了 StreamContent
。有什么方法可以在不使用 MemoryStream
的情况下从 .Net HTTP POST
a FileStream
吗?我可以修改 MS.Internal.InternalWebRequestStream.Write
(请参阅下面的堆栈跟踪)以不使用 MemoryStream
吗?
堆栈跟踪:
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()
解决方案是使用 HttpWebRequest
并按照 here 所述设置 AllowWriteStreamBuffering = false
。
HttpClient.PostAsync(uri, StreamContent)
似乎在幕后使用了 MemoryStream
,即使我使用 FileStream
创建了 StreamContent
。有什么方法可以在不使用 MemoryStream
的情况下从 .Net HTTP POST
a FileStream
吗?我可以修改 MS.Internal.InternalWebRequestStream.Write
(请参阅下面的堆栈跟踪)以不使用 MemoryStream
吗?
堆栈跟踪:
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()
解决方案是使用 HttpWebRequest
并按照 here 所述设置 AllowWriteStreamBuffering = false
。