What is the best practice for IHttpClientFactory and changing HttpClient.BaseAddress?
When creating my Dependency Injection I am doing it like this:
services.AddHttpClient("MyApp", c =>
{
c.BaseAddress = new Uri("https://myurl/");
c.DefaultRequestHeaders.Add("Accept", "application/json");
}).ConfigurePrimaryHttpMessageHandler(handler => new HttpClientHandler()
{ AutomaticDecompression = DecompressionMethods.GZip });
Then when I need an HttpClient doing this:
var client = clientFactory.CreateClient("MyApp");
This works great, but there are times, during runtime that the BaseAddress needs to change. During the run I am not able to change the BaseAddress after it has been injected. Now I could ignore BaseAddress altogether and just send the entire address in the API call, however, I do not know if this is the correct way of doing it. Something like this:
await using var stream = await client.GetStreamAsync($"{addresss}/{api}");
using var streamReader = new StreamReader(stream);
using var textReader = new JsonTextReader(streamReader);
var serializer = new JsonSerializer();
data = serializer.Deserialize<List<T>>(textReader);