This project is read-only.

Stream response

Topics: Web Api
Jan 31, 2012 at 10:30 AM


Are there any examples on how to stream a response....

What I'd like is to call a service, which extract a lot of data from a DB. If I do it the normal way, I'll have to wait for the extract of all the data to be done, before returning anything to the client... what I'd like is to just write the data to a stream, which the client can read right away.... how do I do that?

I would have expected something like this

        public Task<HttpResponseMessage<Stream>> Extract()
            return Task.Factory.StartNew<HttpResponseMessage<Stream>>((HttpResponseMessage<Stream> m) =>
                var sw = new StreamWriter(m.Content);

but I really can't figure out how to do it....

Jan 31, 2012 at 4:29 PM

Download the examples. There are streaming examples in there.

Jan 31, 2012 at 5:26 PM


you could use the type HttpResposneMessage and assign a StreamContent to it...this StreamContent accepts a Stream object.

The type HttpResponseMessage<T> needs to be used in scenarios where you expect the 'T' to be formatted using the formatters like Xml, Json etc....since your case you just would want to return a raw stream, you could use the StreamContent as mentioned above...

hope this helps...

regards, Kiran

Feb 1, 2012 at 7:30 AM


Using a Stream Response, I'd have to do something like this:

        public HttpResponseMessage<Stream> Extract()
            var ms = new MemoryStream();
            var sw = new StreamWriter(ms, Encoding.UTF8);

            for (var n = 1; n != 1000000; ++n) 
            ms.Position = 0;

            var response = new HttpResponseMessage<Stream>(ms, System.Net.HttpStatusCode.OK);
            return response;

but nothing is send to the client until the loop is done.... what I'd like is start sending data to the client, as soon as I start writing to the stream....

Feb 7, 2012 at 7:38 AM

Hi digitalpacman

What samples are you referring to?? I can find one that returns a filestream, but that's not really what I want...

I have a service that extracts data from different places, this takes about 5 min to be done, but it starts generating data right away. So what I'd like, is to start streaming data to the client right away, instead of waiting until all the data are generated.



Feb 7, 2012 at 4:44 PM

advanced/HttpConfiguration.Sample/SetTransferMode is the client part

Feb 7, 2012 at 4:45 PM

advanced/HttpConfiguration.Sample/Services/SampleService.cs ... method GetStream is the server side part

Feb 9, 2012 at 1:00 PM


Though, as I see it, the whole text is written to the stream before it's returned to the client.

What I need it a way to start streaming to the client, before the whole dataset is ready....





Feb 9, 2012 at 4:16 PM

That code yes, but since it's a stream I am pretty sure you can just pass the stream a memory stream or something back. Fiddle with it, see what you can do. Any contributor here that can comment on this?

Feb 27, 2012 at 9:24 AM


Came across this post:

which is exactly what I was looking for....