In our projects, we use one instance of HttpClient Per BaseAddress (Domain) pattern succesfully. For example all requests for api.externalservice.com make from the same HttpClient. Our codes are designed to handle this kind of requests. Now, we need to make calls for two different sub-domains (api1.externalservice.com and api2.externalservice.com). Is one instance of HttpClient still enough to handle requests and responses for this two sub-domains? Or should we need two instance of HttpClient (one for api1.externalservice.com and one for api2.externalservice.com)? This is important point for us to change or not to change our code structure.
Asked
Active
Viewed 1,137 times
1
-
1It depends how you use HttpClient. You don't want to be re-setting default headers or base address but you can use `SendAsync` with new `HttpRequestMessage`s all you want, regardless of threads. See [my blog post](https://contrivedexample.com/2017/07/01/using-httpclient-as-it-was-intended-because-youre-not/) or read the articles linked to at the bottom of it. – Crowcoder Dec 16 '17 at 23:31
-
Thank you. Before I asked this question here, I read your blog post Crowcoder. But my question is a bit different. How to use same HttpClient for the different sub-domains of a domain? In my scenario, BaseAddress is **externalservice.com** and sub-domains that we should access are **api1.externalservice.com** and **api2.externalservice.com** – rodins Dec 17 '17 at 07:08
-
2If there are only a few then you should be fine with one client per subdomain but to use the same client you would not set `BaseAddress` at all, you would set the url on the `HttpRequestMessage` instead. – Crowcoder Dec 17 '17 at 12:28
-
Thank you. It's clear now. I've just understood your blog post's RECIPE: YOU HAVE MANY DIFFERENT API’S TO CALL AND MAYBE YOU EVEN OFTEN ADD NEW ONES WITH EACH SOFTWARE RELEASE section. – rodins Dec 17 '17 at 12:39
1 Answers
1
One instance is fine. Only if you need multithreading - then you should have one instance for each thread.
Update: (Thanks Kirk)
According to this https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx#Anchor_5 Some of the methods are even thread save.
AND note this comment on the page:
HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors.

Markus
- 2,184
- 2
- 22
- 32
-
3In my experience `HttpClient` works fine when shared across threads (https://stackoverflow.com/questions/11178220/is-httpclient-safe-to-use-concurrently) I think the point of this question is whether `BaseAddress` can vary. – Kirk Woll Dec 16 '17 at 23:40
-
We know it's thread safe or to use one instance of HttpClient is good throughout the life of an application. You are right Kirk. I asked for changing BaseAddress of HttpClient especially for sub domains or any other practical way. It's clear too use only one instance per domain. We also use it successfully. But what's the correct way, when there's one domain and two sub-domain of this domain? Is there any trick to use one instance of HttpClient with sub-domains ? Or Are two instances required for each sub-domain? – rodins Dec 17 '17 at 07:01
-
In my scenario, **BaseAddress is externalservice.com** and **sub-domains that we should access are api1.externalservice.com and api2.externalservice.com** . We can go through this example. – rodins Dec 17 '17 at 07:22