In last article, we have seen a basic example showing how http client middleware can be used to create an HttpClient instance using IHttpClientFactory. In this article, we will see an example of what named http clients are and how they can be used.
IHttpClientFactory provides a single place for providing and configuring a logical HttpClient instance. Each HttpClient instance encapsulates the configurations required for HTTP communication (e.g. headers, stream vs buffer mode, etc.).
For calling any APIs, generally there are few settings which are required. For example, if the API is subscription based, then it might be a requirement to send the API key in request headers.
So, every time an HttpClient instance is created, as a developer, we need to set few properties before making the request. If this code is going to be scattered at multiple places, then you might want to have a single place where all these settings can be set. This can be achieved by using the Http Client middleware.
AddHttpClient provides a way to centralize all the HTTP client configurations at one place. Named clients are a good choice when:
Using the middleware, we can name an HTTP instance, and for every named instance, additional configurations can be specified.
I think this method may prove useful in some situations. It might reduce some duplication of code. Also, all required http communication can be configured at a single place using the http client middleware.
The only thing that might not be ideal is – use of string- for specifying name and for getting the instance of HttpClient. But, a constant can be used to make sure there are no runtime errors due to spelling mistakes / variations.
Are you going to use this in any of your project ? Let me know your thoughts.