C#C
C#2y ago
Alex

✅ Configure base address in HttpClient in Dependency Injection

Hi! I want to add http client with configuration for the service
ILocationService

            builder.Services.AddHttpClient<NominatimLocationService>(client =>
            {
                client.BaseAddress = new Uri("https://nominatim.openstreetmap.org");
                client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");
            });

It has an implementation
NominatimLocationService
where I get this http client. The problem is that I get default http client without configuration. What have I done wrong? Also is there a way to configure
HttpClientHander
in
Dependency Injection
? I need it to set automatic decompression
public class NominatimLocationService:ILocationService
{
    private readonly HttpClient _httpClient;

    public NominatimLocationService(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }
}
Was this page helpful?