How to use both IHttpClientFactory and Typed HttpClient?

I have a typed HttpClient and (let's say) another service/class that uses IHttpClientFactory to get a HttpClient instance.

To register IHttpClientFactory, I inject it as services.AddHttpClient(). And to register typed clients, I go with services.AddHttpClient<MyTypedClient>

My question is, do you think there will be a problem if I use both of the use cases. Any impact on HttpMessageHandler pool?

Thanks, Cem.

Upvotes: 1

Views: 910

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239240

Simply, no. You can use whatever you want. IHttpClientFactory handles the collection of HttpMessageHandler instances. When you inject a typed client, what's actually happening is that IHttpClientFactory is invoked, and new HttpClient instance is created, and its handler is set to the either an already created HttpMessageHandler instance or a new instance.

In other words, it's mostly syntactic sugar. IHttpClientFactory is invoked in either case, and it always creates a new HttpClient instance, setting the handler to one that's tracking, based on name. A "typed" client is really just a "named" client, with the type as the name, and all clients are in effect "named" because it's all being stored in a ConcurrentDictionary, where the name is the key.

Upvotes: 4

Related Questions