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);
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.
BaseAddress
can be changed up until the first request is sent. At that point, it is locked down and cannot be changed again. The HttpClient
factory pattern assumes that each injected client has only one BaseAddress
(which may be unset).
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.
Your options are:
BaseAddress
. This is the normal approach if you have a few well-known hosts.BaseAddress
, passing the entire url in each call. This is perfectly permissible.IHttpClientFactory
to pass out HttpClient
instances where each instance can specify its own BaseAddress
. I would only use this more complex approach if you had some code that required a BaseAddress
(e.g., Refit) but needed to use it with a dynamic host.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With