Code Monkey home page Code Monkey logo

Comments (4)

snielsson avatar snielsson commented on June 4, 2024 1

Hi, I usually use injected typed http clients, but today I stumbled on this advice

https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use

Using PooledConnectionLifetime with singleton instead of HttpClientFactory is new to me. I often use singleton registered services and hence have long lived instances, so according to the recommendations I linked, it would be better to use PooledConnectionLifetime instead of the HttpClientFactory.

What do you think?

from timdeschryver.dev.

timdeschryver avatar timdeschryver commented on June 4, 2024

Hi @snielsson good comment!
Using the singleton with PooledConnectionLifetime is fine when you just have a single service you need to communicate with.

I see a HTTP factory as a singleton that's managed by ASP.NET.
I also find it easier to use HTTP factories when you need to work with multiple services, as each client can be configured by its own needs without interfering with the other clients.

from timdeschryver.dev.

ngoquoctoandev avatar ngoquoctoandev commented on June 4, 2024

If you want to use a typed client in a singleton service, the recommened approach is using SocketsHttpHandler as the primary handler, and configuring the PooledConnectionLifetime.

Since the SocketsHttpHandler will handle connection pooling, you can disable recycling at the IHttpClientFactory level by setting HandlerLifetime to Timeout.InfiniteTimeSpan.

services.AddHttpClient<GitHubService>((serviceProvider, client) =>
{
    var settings = serviceProvider
        .GetRequiredService<IOptions<GitHubSettings>>().Value;

    client.DefaultRequestHeaders.Add("Authorization", _settings.GitHubToken);
    client.DefaultRequestHeaders.Add("User-Agent", _settings.UserAgent);

    client.BaseAddress = new Uri("https://api.github.com");
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
    return new SocketsHttpHandler()
    {
        PooledConnectionLifetime = TimeSpan.FromMinutes(15)
    };
})
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);

from timdeschryver.dev.

timdeschryver avatar timdeschryver commented on June 4, 2024

Neat, thanks for the info @ngoquoctoandev

from timdeschryver.dev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.