1

Resolved

PushstreamContent in Win8 App is not working

description

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:

comments

HongmeiG wrote Oct 18, 2012 at 7:30 PM

Moving this bug out for RTM.

HongmeiG wrote Mar 14 at 11:25 PM

I am guessing in user code, it should not close the stream.

HongmeiG wrote Mar 15 at 12:19 AM

See pushstreamcontent sample in http://aspnet.codeplex.com/SourceControl/changeset/view/75a56ec99968#Samples/WebApi/PushContentControllerSample/PushContentController/Controllers/PushContentController.cs

We should not put using around streamwriter, which will dispose the underlying stream