LightInject
LightInject copied to clipboard
What is LightInject's .AddHttpClient() equivalent
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
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.
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)