Web api method calling a another stream content web api method?

asp.net-web-api, c#

Solution

Try this...

 public HttpResponseMessage Get()
        {

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://www.google.ca");

                return httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
                    .ContinueWith((t) =>
                        {
                            return new HttpResponseMessage()
                                {
                                    Content = t.Result.Content
                                };
                        }).Result;

            }

        }

The above code works for me. The downside of this is that because you used `HttpCompletionOption.ResponseContentRead` the entire payload coming from the remote service will be buffered before returning it your client.

If instead you use `HttpCompletionOption.ResponseHeadersRead` the `t.Result.Content` will return a network stream instead. That way, when your client pulls the response stream it will be streaming directly from the remote resource. I'm assuming that's what you are currently trying to do with the PushStreamContent that you are returning.

Problem

Here is my situation I have two web api services. The first one has a method that processes some data and when part of the data is ready it pushes it to the stream. ``` public HttpResponseMessage Get( ) { var response = Request.CreateResponse( ); response.Content = new PushStreamContent( WriteToStream ); return response; } public async void WriteToStream( Stream outputStream, HttpContent content, TransportContext context ) { ....... } ``` The second web api has a method in a controller that calls the first web api. It must read the part from the stream do something with it and put i to a stream of it's own: ``` public HttpResponseMessage Get( ) { Stream stream = null; using( HttpClient httpClient = new HttpClient( ) ) { httpClient.Timeout = TimeSpan.FromMilliseconds( Timeout.Infinite ); HttpRequestMessage request = new HttpRequestMessage( HttpMethod.Get, "http://localhost/fistWebApi/" ); var response = httpClient.SendAsync( request, HttpCompletionOption.ResponseContentRead ); var content = response.Result.Content.ReadAsStreamAsync( ); stream = content.Result; } var msg = Request.CreateResponse( ); msg.Content = new PushStreamContent( WriteToStream ); return msg; } public async void WriteToStream( Stream outputStream, HttpContent content, TransportContext context ) { ....... } ``` The problem is that I don't know how to synchronize calls `SendAsync` `ReadAsStreamAsync`. Now it doesn't wait and `stream` is not initialized correctly when trying to use it. I want the code of the second method wait until first method puts part of data on the stream. I tried using `ContinueWith` but then I wasn't able to do it. I know it is a little blur explanation if there are any questions I will try to explain more deeply.

Original source