Search code examples
c#asp.net-coredependency-injectiondryioc

Use IServiceCollection extensions with DryIoc


There are many extensions for the IServiceCollection - in my case I use AddHttpClient.

My scenario is that I register general stuff in the ConfigureServices method in the Startup.cs where IServiceCollection is used to register services. Everything that is needed only in specific projects is registered in an extension method in the respective project, but there the DryIoc IContainer is used due to how the DryIoc container must be integrated in an ASP .NET Core project.

Now I have a HttpClient that I only need in a specific project. Therefore I would like to put the registration for it in the respective project. Problem is I want to use AddHttpClient for it which I normally can only use with IServiceCollection.

My question: Is there any way to use it in my other project. Maybe by getting it from the DryIoc container or something else.

This is the general structure of the described files:

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.RegisterSomeService();
        // register other stuff
    }
}

Program.cs

public class Startup
{
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseServiceProviderFactory(new DryIocServiceProviderFactory())
            .ConfigureContainer<Container>(SomeProject.ContainerSetup.Initialize);
}

ContainerSetup.cs in SomeProject

public static class ContainerSetup
{
    public static void Initialize(HostBuilderContext hostContext, IContainer container)
    {
        container.Register<SomeService>();
        // register other stuff
        // here I want to use AddHttpClient
    }
}

Solution

  • I was able to solve the problem by using the IContainer extension Populate which is part of DryIoc.Microsoft.DependencyInjection.

    With it I edited my ContainerSetup.cs as follows

    public static class ContainerSetup
    {
        public static void Initialize(HostBuilderContext hostContext, IContainer container)
        {        
            var services = new ServiceCollection();
            
            services.AddHttpClient<MyTypedClient>()
                .Configure[...];
            
            container.Populate(services); // with this call all services registered above will be registered in the container
            
            // register other stuff if necessary
            container.Register<SomeService>();
        }
    }