LightInject icon indicating copy to clipboard operation
LightInject copied to clipboard

What is LightInject's .AddHttpClient() equivalent

Open ArnaudB88 opened this issue 10 months ago • 2 comments

What is the LightInject equivalent for the recommened registration of HttpClients:

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
IServiceCollection services = builder.Services;

services.AddHttpClient("my-client")
        .AddStandardResilienceHandler(options =>
        {
            // Configure standard resilience options here
        });

And the resolve:

// Use the client
var host = builder.Build();
var httpClient = host.Services
    .GetRequiredService<IHttpClientFactory>()
    .CreateClient("my-client");

Source: https://devblogs.microsoft.com/dotnet/building-resilient-cloud-services-with-dotnet-8/#tl;dr

Thanks

ArnaudB88 avatar Dec 27 '24 14:12 ArnaudB88

Hi @ArnaudB88

Since LightInject is a drop-in replacement for the built-in container, http clients are registered exactly the same way as if we used the built-in container.

seesharper avatar Jan 14 '25 23:01 seesharper

Hi @seesharper

Do you mean that registered httpClients on the MS.DI container can also be resolved from the LightInject container?

My actual question was if it is possible to have similar code (to the one above), but for LightInject. That way we can registerd http clients independend of the MS.DI container. But after some research, I found similar github tickets of autofac and simpleInjector which explain that the http client registration (and resilience) is tightly coupled with the MS.DI container. So if we want http clients to be registered correctly when using LightInject, I think we have 2 options:

  • register HTTP clients on the MS.DI container and resolve them with lightinject (if possible)
  • register singleton http clients directly on the LightInject container and don't use the http client factory (see ms docs)

ArnaudB88 avatar Jan 15 '25 14:01 ArnaudB88