Skip to content

Instantly share code, notes, and snippets.

@Ilchert
Created October 12, 2023 10:28
Show Gist options
  • Save Ilchert/6777a74837248d706e7cd2c242e4b042 to your computer and use it in GitHub Desktop.
Save Ilchert/6777a74837248d706e7cd2c242e4b042 to your computer and use it in GitHub Desktop.
// 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