I am hitting an issue with using PushStreamContent in the Windows store app. I don’t see the same issue in the traditional console app though.
It’s a simple scenario where I am ‘POST’ing some small data to a service using PushStreamContent as below.
HttpClient client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "http://.../SimpleMvcWebAPIApplication/api/values");
request.Content = new PushStreamContent((stream, httpContent, transportContext) =>
{
using (var streamWriter = new StreamWriter(stream))
{
string data = "\"hello world\"";
streamWriter.Write(data);
streamWriter.Flush();
}
}, "application/json");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
It fails as below.
System.ObjectDisposedException was unhandled by user code
HResult=-2146232798
Message=Cannot access a closed Stream.
Source=mscorlib
ObjectName=""
StackTrace:
at System.IO.__Error.StreamIsClosed()
at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc)
at System.Net.Http.HttpContent.<>c__DisplayClassf.<LoadIntoBufferAsync>b__d(Task copyTask)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at App5.App.<SimpleWebAPIService_Write_Temp1>d__f.MoveNext() in c:\personal\maying\WinRT\client\WinRTClient\App.xaml.cs:line 368
InnerException:
It’s a simple scenario where I am ‘POST’ing some small data to a service using PushStreamContent as below.
HttpClient client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "http://.../SimpleMvcWebAPIApplication/api/values");
request.Content = new PushStreamContent((stream, httpContent, transportContext) =>
{
using (var streamWriter = new StreamWriter(stream))
{
string data = "\"hello world\"";
streamWriter.Write(data);
streamWriter.Flush();
}
}, "application/json");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
It fails as below.
System.ObjectDisposedException was unhandled by user code
HResult=-2146232798
Message=Cannot access a closed Stream.
Source=mscorlib
ObjectName=""
StackTrace:
at System.IO.__Error.StreamIsClosed()
at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc)
at System.Net.Http.HttpContent.<>c__DisplayClassf.<LoadIntoBufferAsync>b__d(Task copyTask)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at App5.App.<SimpleWebAPIService_Write_Temp1>d__f.MoveNext() in c:\personal\maying\WinRT\client\WinRTClient\App.xaml.cs:line 368
InnerException: