Akhil
Akhil

Reputation: 2030

IHttpClientFactory Singleton .NET Framework

Scenario

I am trying to change my existing HttpClient to IHttpClientFactory. When I verified the existing code, its using using{...} statement which causes issues and it is mentioned here. So I thought of implementing singleton Http client and reached another blog related to this and it is here.

From all these, I understood that the best one is IHttpClientFactory introduced in .NET Core.

Implementation Plan

As this application is in ASP.NET MVC 4 and does not use DI, I have to do something to use without the DI framework. Based on my search, got answers from StackOverflow and planned to implement the same way. Meanwhile, I also got another project, which already removed all the dependencies and is ready to use in earlier projects without doing all things. The repo is HttpClientFactoryLite.

Question

Now I can use HttpClientFactoryLite by initializing this class? The description also mentioned it can be used along with the existing DI framework so that ClientFactory can be registered as a singleton. Please find the wordings from the readme

using HttpClientFactoryLite;

var httpClientFactory = new HttpClientFactory(); //bliss

If you are using dependency injection, make sure that IHttpClientFactory is registered as a singleton.

In my scenario, I don't have any DI framework added. So I am going to initialize the factory wherever I needed. Here I am confused that in 2 things

  1. Is it necessary to make a singleton class for HttpClientFactoryLite?

  2. How is this HttpClientFactory class disposed? Is there a need to dispose of it as part of the controller or same using statement etc?

  3. Based on the answer from this, Microsoft.Extensions.Http provides the HttpClientFactory only, not the new optimized HttpClient. This is only available in .NET Core 2.1. So any difference in implementing IHttpClientFactory?

Please advise

Upvotes: 3

Views: 11204

Answers (1)

Purity
Purity

Reputation: 331

ASP.NET 3.1:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddSingleton<IHttpClientFactory, HttpClientFactory>();
}

ASP.NET will automatically pass the correct singleton to controllers which demand an IHttpClientFactory in their constructor.


Poormans variation without DI-Container:

public static class Singleton<TInterface> 
{
    private static TInterface instance;
    public static TInterface Instance
    { 
        get => instance;
        private set => instance ??= value;
    }

    public static void Add<TConcrete>() where TConcrete : TInterface, new()
        => Instance = new TConcrete();

    public static void Add<TConcrete>(TConcrete instance) where TConcrete : TInterface
        => Instance = instance;

    // put dispose logic if necessary
}

Usage:

// Application Entrypoint
Singleton<IHttpClientFactory>.Add<HttpClientFactory>();

// Class/Controller Property
private readonly IHttpClientFactory httpClientFactory 
    = Singleton<IHttpClientFactory>.Instance;

Upvotes: 4

Related Questions