using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
public static void Main()
var serviceCollection = new ServiceCollection();
.AddHttpClient<ITypedClient, TypedClientA>(
httpClient.BaseAddress = new Uri("http://fake.local/address/1");
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.Add("caller", "caller");
httpClient.DefaultRequestHeaders.Add("actor", nameof(TypedClientA));
.AddHttpClient<ITypedClient, TypedClientB>(
httpClient.BaseAddress = new Uri("http://fake.local/address/2");
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.Add("caller", "caller");
httpClient.DefaultRequestHeaders.Add("actor", nameof(TypedClientB));
var serviceProvider = serviceCollection.BuildServiceProvider();
var allTypedClients = serviceProvider.GetServices<ITypedClient>();
foreach (var tc in allTypedClients)
public interface ITypedClient
public class TypedClientA : ITypedClient
private readonly HttpClient _httpClient;
public TypedClientA(HttpClient httpClient)
_httpClient = httpClient;
Console.WriteLine(nameof(TypedClientA));
Console.WriteLine(_httpClient.BaseAddress);
Console.WriteLine(_httpClient.DefaultRequestHeaders.ToString());
public class TypedClientB : ITypedClient
private readonly HttpClient _httpClient;
public TypedClientB(HttpClient httpClient)
_httpClient = httpClient;
Console.WriteLine(nameof(TypedClientB));
Console.WriteLine(_httpClient.BaseAddress);
Console.WriteLine(_httpClient.DefaultRequestHeaders.ToString());