Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Should I explicitly use the httpClientFactory class as a recommendation when creating and using typed http clients and DI in .NET 6?

I am using NSwag to create typed http clients with interfaces and registering them with DI using AddHttpClient() and injecting them where they need to be used in calling APIs.

I read about some limitations of the httpclient class such as socket exhaustion & not detecting dns changes and the recommendation to use httpClientFactory and its CreateClient method to create httpclients to alleviate these limitations.

I haven't used the httpClientFactory in a new app that I started working on. It's using .NET 6 and my question is.. Is httpClientFactory still a recommended way to use explicitly or is it used implicitly in DI because of the use of AddHttpClient() and I don't have to worry about those limitations? I didn't see a clear direction on how to use typed clients, or http clients in general, and whether .NET 6 has introduced improvements in this area.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  • HttpClient can be injected only inside typed clients
  • For other usages IHttpClientFactory is required

As the docs state for either case underlying infrastructure (HttpMessageHandler) is managed by the framework so in general you don't need to worry about it (unlike when creating and managing HttpClients manually which could lead to incorrect disposal).

Using typed clients registered via HttpClientFactoryServiceCollectionExtensions.AddHttpClient<TClient> handles IHttpClientFactory for you. When adding typed client internally AddTypedClientCore is called which registers resolver via AddTypedClientCore, which uses IHttpClientFactory:

IHttpClientFactory httpClientFactory = s.GetRequiredService<IHttpClientFactory>();
HttpClient httpClient = httpClientFactory.CreateClient(builder.Name);
 
ITypedHttpClientFactory<TClient> typedClientFactory = s.GetRequiredService<ITypedHttpClientFactory<TClient>>();
return typedClientFactory.CreateClient(httpClient);

Related:

  • Make HTTP requests using IHttpClientFactory in ASP.NET Core
  • Use IHttpClientFactory to implement resilient HTTP requests
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!