/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/core/extensions/snippets/http/configurehandler/TodoService.cs
37 строк
1 KB
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.Configuration; using Microsoft.Extensions.Logging; using Shared; namespace ConfigureHttpHandler.Example; public sealed class TodoService( IHttpClientFactory httpClientFactory, IConfiguration configuration, ILogger<TodoService> logger) { public async Task<Todo[]> GetUserTodosAsync(int userId) { // Create the client const string name = "ConfigureHttpHandler.Example"; using HttpClient client = httpClientFactory.CreateClient(name); try { // Make HTTP GET request // Parse JSON response deserialize into Todo type Todo[]? todos = await client.GetFromJsonAsync<Todo[]>( $"todos?userId={userId}", new JsonSerializerOptions(JsonSerializerDefaults.Web)); return todos ?? []; } catch (Exception ex) { logger.LogError("Error getting something fun to say: {Error}", ex); } return []; } }