Examples exist to read data from an IAsyncEnumerator to the UI in a Blazor app using an internal service. Examples also exist on how to send an IAsyncEnumerator as an output from a Web API Controller action.
I haven't seen any examples yet how to read an IAsyncEnumerator stream from an API using an HttpClient within a client like Blazor or Xamarin. Everything I've tried so far, only returns the HttpResponseMessage on the client after the async/await foreach loop on the API is done.
HttpResponseMessage response = await _httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
What should the content type be (Produces attribute) on the HttpGet action? What should the Accept value be on the request header from the HttpClient?
[HttpGet]
[Produces("application/json")]
public async IAsyncEnumerable<Data> Get(CancellationToken cancellationToken)
{
var dbSet = _dbContext.TableName.AsNoTracking().AsAsyncEnumerable().WithCancellation(cancellationToken).ConfigureAwait(false);
await foreach (var i in dbSet.ConfigureAwait(false))
{
var item = new Data
{
Id = i.Id,
Name = i.Name
};
yield return item;
}
}