When writing an application that uses HttpClient I have the same approach as this post, in other words I don't use using and instead use a static HttpClient. I have had no problems with that when I want to communicate with only one server. (I set the Ip address as BaseAddress
and proceed)
Now I have the same problem as this question regarding the fact that BaseAddress can not be changed after starting to use the HttpClient.
That question's answers respond with an explanation that it can not be done. You can not change BaseAddress.
So my question (which is different than the linked one so not a duplicate) is what to do if we want to change the ip adress to communicate with other server?
Should we instantiate another HttpClient? (no, we are not going to use using
) How do we correctly proceed from here?
BaseAddress
property has a setter so you CAN change it (in .NET 5) but only BEFORE you send the first request (after that you'll get an exception, see the source code specifically the call toCheckDisposedOrStarted()
on line 87).