In last article, we have seen how a named HttpClient instance can be useful. It allows to keep all configurations at single place. These configurations are associated with a name. Then the name can be passed as parameter to IHttpClientFactory.CreateClient method to obtain the named instance.
Conceptually, typed HttpClient instances also works on the same lines. But instead of using a string, it uses a type. In this blogpost, let’s see how to use typed HttpClient instances.
Typed vs Named Clients
As explained in previous post, a string can be used as key while configuring named HttpClient instances. Typed HttpClients also provide the same facility. But instead of using a string as a key, it enables to use a type as a key.
With named clients, we had to inject IHttpClientFactory so that CreateClient method can be called to create an HttpClient instance. This is where typed instances are a bit better. The registered type can directly be injected as dependency.
For ex. let’s say an application needs to call GitHub API and Google Maps API. A single dedicated type can be used for a single backend type, meaning a class
GitHubApiConsumer and another class
GoogleMapsApiConsumer can be created to call these backend APIs. These two classes will encapsulate all the necessary details which are required to call the respective backend service.
The caller does not need to know about IHttpClientFactory or any other HttpClient configurations.
Typed client basically means a type, which accepts HttpClient as a dependency in its constructor. Below code shows a type to call GitHub API. Not that, the HttpClient instance is not exposed to outside classes.
Below code does not specify any configurations like request headers or base address. For this demo, these settings are specified in
Next code block is showing
Startup.cs configurations and a
HomeController.cs code showing how this typed client can be consumed.
I would personally prefer typed clients over named clients, because
- Typed client can be injected directly
- Typed clients encapsulates all the logic of calling backend API, thus also helping improving readability and maintainability of the application.
What is your opinion ? Let me know your thoughts.