HttpClient get JSON as AsyncEnumerable
A new extension method – GetFromJsonAsAsyncEnumerable<T>
– has been added to the HttpClient
part of .NET 8. This new mthod will take the response body JSON and deserialize it into an async enumerable operation.
The complete signature of the extension method is as follows:
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
public static IAsyncEnumerable<TValue?> GetFromJsonAsAsyncEnumerable<TValue>(
this HttpClient client,
[StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri,
CancellationToken cancellationToken = default) =>
GetFromJsonAsAsyncEnumerable<TValue>(client, requestUri, options: null, cancellationToken);
Code language: C# (cs)
Let’s take a look at how to use it. First thing to note is that the GetFromJsonAsAsyncEnumerable
is not async
, as the async part is being handled by the IAsyncEnumerable
returned.
IAsyncEnumerable<Hotel> hotels = client.GetFromJsonAsAsyncEnumerable<Hotel>("https://foo.bar/api/hotels");
Code language: C# (cs)
Next, we take the hotels
result and use an await foreach
to await and iterate on each element of the IAsyncEnumerable
.
await foreach (var hotel in hotels)
{
Console.WriteLine($"{hotel.stars}* | {hotel.name}");
}
Code language: C# (cs)
Inside the body of the foreach
you can do anything you want want with your hotel
. Full example below:
using System.Net.Http.Json;
using var client = new HttpClient();
IAsyncEnumerable<Hotel> hotels = client.GetFromJsonAsAsyncEnumerable<Hotel>("https://foo.bar/api/hotels");
await foreach (var hotel in hotels)
{
Console.WriteLine($"{hotel.stars}* | {hotel.name}");
}
public record Hotel(string name, string address, int stars);
Code language: C# (cs)
One Comment