Created
October 12, 2023 10:28
-
-
Save Ilchert/6777a74837248d706e7cd2c242e4b042 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://aka.ms/new-console-template for more information | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
var builder = Host.CreateApplicationBuilder(args); | |
builder.Services.AddSingleton<Service>(); | |
builder.Services.AddHttpClient(Service.HttpClientKey) | |
.ConfigureHttpClient((sp, c) => | |
{ | |
var configuration = sp.GetRequiredService<IConfiguration>(); | |
c.BaseAddress = new Uri(configuration.GetValue<string>("ServiceAddress")); | |
}); | |
Console.ReadLine(); | |
class Service(IHttpClientFactory httpClientFactory) | |
{ | |
public const string HttpClientKey = "Service"; | |
public async Task<string> GetData(CancellationToken ct = default) | |
{ | |
using var client = httpClientFactory.CreateClient(HttpClientKey); | |
return await client.GetStringAsync("trololo", ct); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment