/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/core/extensions/snippets/http/generated/Program.cs
38 строк
1 KB
David Pine
Replace Host creation, and fix #34867 (#35413)
22 май 2023, 20:07
Не верифицирован
22 май 2023, 20:07
c520d5b
Код
Авторство
О чём код?
using GeneratedHttp.Example; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Refit; using Shared; HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); builder.Services.AddRefitClient<ITodoService>() .ConfigureHttpClient(client => { // Set the base address of the named client. client.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/"); // Add a user-agent default request header. client.DefaultRequestHeaders.UserAgent.ParseAdd("dotnet-docs"); }); using IHost host = builder.Build(); ITodoService todoService = host.Services.GetRequiredService<ITodoService>(); Todo[] todos = await todoService.GetUserTodosAsync(2); ILogger logger = host.Services.GetRequiredService<ILogger<ITodoService>>(); foreach (Todo? todo in todos) { logger.LogInformation("Todo: {Details}", $""" Id: {todo?.Id} (Is completed: {todo?.Completed}) Title: {todo?.Title} """); } await host.RunAsync();