13

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?

3
  • 4
    then do not set base address and just use the full addresses for the requests. That way the same http client can be used for all requests other wise you would need to create a new client for each base address which will also defeat the purpose of having the single client.
    – Nkosi
    Commented Sep 6, 2018 at 3:14
  • actually you CAN change BaseAddress. At least in my net core 3 project I do. _httpClient.BaseAddress = new Uri(newAddress); Commented Jun 5, 2020 at 7:52
  • The 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 to CheckDisposedOrStarted() on line 87).
    – Eric Mutta
    Commented Jul 28, 2021 at 20:22

2 Answers 2

18

As it stands you can't change the base address.

How do we correctly proceed from here?

Do not set the base address and just use the full addresses for the requests.

That way the same client can be used for all requests other wise you would need to create a new client for each base address which will also defeat the advantages of having the single client.

The client factory in asp.net core 2+ has since fixed the problems associated with having multiple clients.

Disposing of the client is not mandatory, but doing so will cancel any ongoing requests and ensure the given instance of HttpClient cannot be used after Dispose is called. The factory takes care of tracking and disposing of the important resources that instances of HttpClient use, which means that HttpClient instances can be generally be treated as .NET objects that don’t require disposing.

One effect of this is that some common patterns that people use today to handle HttpClient instances, such as keeping a single HttpClient instance alive for a long time, are no longer required. Documentation about what exactly the factory does and what patterns it resolves will be available, but hasn’t been completed yet.

Completed documentation Use HttpClientFactory to implement resilient HTTP requests

1
  • So I guess the next question would be, if somehow I want the IP address of the server taken from settings and not changed during running, what would be the best approach? (I guess reading a text file is possible but is it the best way?) Commented Sep 6, 2018 at 4:02
1

I was able to change the BaseAddress by making a new one instance of HttpClient.

Let's say you have a handle holding the HttpClient in your class.

public HttpClient Client { get; set; }

Then in your code:

public void InitializeMyStuffAndFetchBaseAddress() {
  Client = new HttpClient();
  // do something with the Client, let's say we
  // wanna fetch what's the base address we should use
  string url = "https://www.myserver.com/getaccess";
  string baseAddress = await Client.GetStringAsync(url);
  ...
  Client.Dispose();
  
  Client = new HttpClient();
  Client.BaseAddress = new Uri(baseAddress );
  ...
}

So in terms of the code using the Client is transparent to them even if you spawn a new instance of HttpClient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.