Last post Mar 06, 2016 04:27 PM by Matan Edri
Feb 14, 2016 11:35 AM|Matan Edri|LINK
I'm trying to utilize the PushStreamContent class to stream results of a big query back to the client.
I noticed that when I write results to the stream in the delegate passed to the ctor, the client never starts reading them until the server finishes writing all results to the stream and calls its Close() method.
After looking at some examples I couldn't figure out what I'm doing wrong, until I noticed something strange.
When debugging the client, the stream that I receive is always System.IO.MemeoryStream, meaning that HttpClient returns this stream from the call to HttpContent.ReadAsStreamAsync, and reads the entire response to that memory stream before the app can handle
I found out that this happens because my request is sent as POST (I need to pass a complex object as a parameter to the controller action).
When I execute a GET, everything suddenly started to work.
Soooo, is this by design?
If so, how can I perform a GET with a complex object passed to the controller's GET method?
Apparently this happens whenever you call HttpContent.ReadAsStreamAsync(), no matter if the method is POST or GET.
The only way streaming works is when I call HttpClient.GetStreamAsync().
Is there any way I could achieve this with a POST request?
Feb 15, 2016 05:34 AM|Yohann Lu|LINK
Hi Matan Edri,
From your description, I suggest you can try the following solution.
You can add custom handlers to the pipeline. Message handlers are good for cross-cutting concerns that operate at the level of HTTP messages (rather than controller actions). You can refer the following code.
public class MessagessssHandler : DelegatingHandler
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken
var requestMessage = await request.Content.ReadAsStreamAsync();
//do something with requestMessage - but you will have to deserialize from Srteam
return await base.SendAsync(request, cancellationToken);
You can add a message handle inside the WebApiConfig class:
Information about HTTP Message Handlers in ASP.NET Web API for your reference:
Mar 06, 2016 04:27 PM|Matan Edri|LINK
Actually the easier, more correct way to do this was to use the
HttpClient.SendAsync overload which accepts the HttpCompletionOptions as a parameter, and use HttpCompletionOption.ResponseHeadersRead value to tell the client to complete the request once headers are read and then the ReadStreamAsync actually returns the
network stream that enables streaming the response content.