A named HttpClient
is registered in Startup.cs
as services.AddHttpClient("MainClient")
.
The HttpClient
is used like this in different places:
var httpClient = _httpClientFactory.CreateClient("MainClient");
httpClient.BaseAddress = new Uri(_customUrl);
httpClient.DefaultRequestHeaders.Add("CustomHeader", _customHeader);
var result = await httpClient.SendAsync(...);
The properties _customUrl
and _customHeader
vary in different places.
Is there a potential race condition when the httpClient
is used in different HTTP-requests (or in parallel in multithreading) simultaneously with different BaseAddress
and DefaultRequestHeaders
?
Is there a way to avoid the race condition? Should lock/semaphoreslim
be used in each code parts when httpClient is used?