Aspnetcore.docs: Typed client, an instance of `HttpClient` is created

Created on 4 Mar 2020  ·  5Comments  ·  Source: dotnet/AspNetCore.Docs

In the Typed clients section, the docs are stating:

The typed client is registered as transient with DI. In the preceding code, AddHttpClient registers GitHubService as a transient service. This registration uses a factory method to:

  1. Create an instance of HttpClient.
  2. Create an instance of GitHubService, passing in the instance of HttpClient to its constructor.

Is it, that only the typed client is injected as transient, but a new instance of the HttpClient is created once for each typed client and then reused?

I believe if it happens otherwise, it would contradict with the "Improper Instantiation antipattern"...


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

PU Source - Docs.ms doc-enhancement

All 5 comments

Let's ask @rynowak or @stevejgordon.

I'm certainly not Ryan Nowak or Steve Gordon, but I think Steve's HttpClient Creation and Disposal Internals: Should I Dispose of HttpClient? post should help:

At this point, I can’t state strongly enough that DefaultHttpClientFactory.CreateClient(…) will always provide a new HttpClient instance. This can be confusing since its easy to tie the connection lifetime issue with HttpClient. In reality, the connection lifetime is more directly tied to the HttpMessageHandler level.

Typed Clients use DefaultHttpClientFactory.CreateClient(…) too, so it's always a new HttpClient, but it's the management of HttpMessageHandler that matters. That's all handled correctly by HttpClientFactory.

@serpent5 is correct. Internally the HttpClientFactory feature pools and manages the lifetime of the HttpMessageHandler instances. Instances of HttpClient are always newly created by the factory.

Typed clients are transient for that reason as it ensures they always get a new HttpClient instance, which may or may not have an existing chain of HttpMessageHandler.

Most of that I'd consider implementation detail and the important point is understanding that the typed client is resolved from DI as a transient service and should not be captured by a singleton service for example.

Thank you all for the answers, it really helped clear things out, but wouldn't you think that a note of this should exist in the documents?

For us devs, understanding why we are doing something is very important and just by reading the docs you can get easily confused. I mean, without knowing all about these implementation details, reading alone the "Improper Instantiation antipattern" and then the statement that a typed client is resolved as transient can throw you off track!

I think anyone should be able to deep dive and understand without reaching content outside Microsoft Docs...

In any case, @serpent5 and @stevejgordon, thank you again very much for your responses, you really helped me.

For us devs, understanding why we are doing something is very important and just by reading the docs you can get easily confused. I mean, without knowing all about these implementation details, reading alone the "Improper Instantiation antipattern" and then the statement that a typed client is resolved as transient can throw you off track!

If you want to suggest some specific improvements I think we'd be happy to make them. 👍

In our minds we try to make things as clear as possible without over-explaining. If we end up with the docs as a "wall of text" it's equally bad.

Was this page helpful?
0 / 5 - 0 ratings