title: Consuming an IAsyncEnumerable
body: <p>I have one web API application that has a function that needs to stream some data. Then I have another web application that basically acts like a proxy for that. This other application does a whole lot of other stuff like handling authorization and such. It needs to connect to the API endpoint and pass the stream through potentially doing some addition processing for each row of data. In one case it will need to write out that data into an Excel file, for example.</p>
  <p>Unfortunately, I see a lot of tutorials on how to create an IAsyncEnumerable, but not any about actually connecting to an endpoint that exposes one.</p>
  <p>Ideally, I would like to actually wrap my IAsyncEnumerable and return it with some other <em>stuff</em>. This is what I basically do with other non-streamed endpoints already:</p>
  <pre><code>public class NonStreamedResult : BaseResult
  {
      public SomeMetaData MetaData {get; set; }
      // here Data is usually small, so returning it all at once is no big deal
      public IEnumerable&lt;SomeData&gt; Data {get; set; }  
  }
  
  //....controller
  public async Task&lt;NonStreamedResult&gt; GetNonStreamedData()
  </code></pre>
  <p>And on the consuming side, I'm using an <code>HttpClient</code>, awaiting an <code>HttpResponseMessage</code> and using <code>ReadAsAsync&lt;T&gt;</code> to turn that response into an object.</p>
  <p>However, if I try to do this:</p>
  <pre><code>public class StreamedResult : BaseResult
  {
      public SomeOtherMetaData MetaData {get; set; }
      // Here data could be huge, so we don't want to have to hold it all in memory at once
      public IAsyncEnumerable&lt;SomeData&gt; Data {get; set; }
  }
  
  //....controller
  public async Task&lt;StreamedResult&gt; GetStreamedData()
  </code></pre>
  <p>I will get an error when it tries to deserialize because it doesn't know what concrete class to convert the <code>IAsyncEnumerable</code> into.</p>
  <p>So what's the right way to connect to an endpoint that is using an <code>IAsyncEnumerable</code> <em>without</em> loading the entire response into memory (obviously) so that I can eventually return another <code>IAsyncEnumerable</code> from the proxy app?</p>
  <p>It would be nice if I could still pass it wrapped, but it's not the end of the world if I have to work with just the naked <code>IAsyncEnumerable</code></p>
  <p>Note that I'm using the <code>Microsoft.Bcl.AsyncInterfaces</code> NuGet package since I'm still targeting an earlier version of the .NET framework.</p>