/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/core/extensions/snippets/http/basic/TodoService.cs
34 строки
958 B
David Pine
Upgrade TFMs to .NET 8.0, and packages (#38752)
15 дек 2023, 16:49
Не верифицирован
15 дек 2023, 16:49
114b8f9
Код
Авторство
О чём код?
using System.Net.Http.Json; using System.Text.Json; using Microsoft.Extensions.Logging; using Shared; namespace BasicHttp.Example; public sealed class TodoService( IHttpClientFactory httpClientFactory, ILogger<TodoService> logger) { public async Task<Todo[]> GetUserTodosAsync(int userId) { // Create the client using HttpClient client = httpClientFactory.CreateClient(); try { // Make HTTP GET request // Parse JSON response deserialize into Todo types Todo[]? todos = await client.GetFromJsonAsync<Todo[]>( $"https://jsonplaceholder.typicode.com/todos?userId={userId}", new JsonSerializerOptions(JsonSerializerDefaults.Web)); return todos ?? []; } catch (Exception ex) { logger.LogError("Error getting something fun to say: {Error}", ex); } return []; } }