HttpClient JSON als AsyncEnumerable abrufen
Die neue Erweiterungsmethode GetFromJsonAsAsyncEnumerable in .NET 8 deserialisiert den JSON-Body einer HTTP-Antwort in ein IAsyncEnumerable. Erfahren Sie, wie Sie sie mit await foreach einsetzen.
Mit .NET 8 wurde dem HttpClient eine neue Erweiterungsmethode hinzugefügt: GetFromJsonAsAsyncEnumerable<T>. Sie nimmt den JSON-Body der Antwort und deserialisiert ihn in eine asynchrone Enumeration.
Die vollständige Signatur der Erweiterungsmethode lautet:
[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);
Schauen wir uns den Einsatz an. Zuerst fällt auf, dass GetFromJsonAsAsyncEnumerable selbst nicht async ist, denn der asynchrone Teil wird über das zurückgegebene IAsyncEnumerable abgewickelt.
IAsyncEnumerable<Hotel> hotels = client.GetFromJsonAsAsyncEnumerable<Hotel>("https://foo.bar/api/hotels");
Anschließend nehmen wir das Ergebnis hotels und iterieren mit einem await foreach asynchron über jedes Element des IAsyncEnumerable.
await foreach (var hotel in hotels)
{
Console.WriteLine($"{hotel.stars}* | {hotel.name}");
}
Innerhalb des foreach-Bodys können Sie mit Ihrem hotel machen, was Sie möchten. Vollständiges Beispiel unten:
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);
Comments
Sign in with GitHub to comment. Reactions and replies thread back to the comments repo.