C
C#9mo ago
Alex

AddHttpClient configuration is never executed

I want to register HttpClient in DI container to use it in service to send request to other api. The configureHttpClient is not executed and configuration values are not set in the HttpClient and I get error that I can't send request because baseAddress is not set. Here I register HttpClient for ILocationService,NominatimLocationService
builder.Services.AddHttpClient<ILocationService,NominatimLocationService>()
.ConfigureHttpClient(client =>
{
client.BaseAddress = new Uri("https://nominatim.openstreetmap.org");
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");
});
builder.Services.AddHttpClient<ILocationService,NominatimLocationService>()
.ConfigureHttpClient(client =>
{
client.BaseAddress = new Uri("https://nominatim.openstreetmap.org");
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");
});
Here I register services
services.AddScoped<ILocationService, NominatimLocationService>();
services.AddScoped<ILocationService, NominatimLocationService>();
Here is a class that implements ILocationService
public class NominatimLocationService:ILocationService
{
private readonly HttpClient _httpClient;

public NominatimLocationService(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<IEnumerable<LocationResponse>> FindByQueryAsync(string query)
{
// https://nominatim.openstreetmap.org/search.php?q=tes&format=jsonv2
HttpResponseMessage responseMessage = await _httpClient.GetAsync($"search.php?q={query}&format=jsonv2");
...
}
}
public class NominatimLocationService:ILocationService
{
private readonly HttpClient _httpClient;

public NominatimLocationService(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<IEnumerable<LocationResponse>> FindByQueryAsync(string query)
{
// https://nominatim.openstreetmap.org/search.php?q=tes&format=jsonv2
HttpResponseMessage responseMessage = await _httpClient.GetAsync($"search.php?q={query}&format=jsonv2");
...
}
}
And here how I use class
public class LocationsController : ControllerBase
{
private readonly ILocationService _locationService;

public LocationsController(ILocationService locationService)
{
_locationService = locationService;
}

[HttpGet("search")]
public async Task<IActionResult> Search([FromQuery] string? query)
{
if(string.IsNullOrEmpty(query))
{
throw new BadRequestException("Search parameter 'query' is required");
}

IEnumerable<LocationResponse> locations = await _locationService.FindByQueryAsync(query);

return Ok(locations);
}
}
public class LocationsController : ControllerBase
{
private readonly ILocationService _locationService;

public LocationsController(ILocationService locationService)
{
_locationService = locationService;
}

[HttpGet("search")]
public async Task<IActionResult> Search([FromQuery] string? query)
{
if(string.IsNullOrEmpty(query))
{
throw new BadRequestException("Search parameter 'query' is required");
}

IEnumerable<LocationResponse> locations = await _locationService.FindByQueryAsync(query);

return Ok(locations);
}
}
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server