Use existing IContainer in new instance of Autofac container

616 views Asked by At

I have solution with .NET Core 3.1 console application, which using Autofac as IoC container. In this solution is approximately 50 projects as class libraries, which is referenced over Autofac as modules. Now I am creating new module, which will start .NET Core web API which will provide data from database and other running modules.

New web API module is declared as follows:

public class RESTModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(c =>
        {
            return Host.CreateDefaultBuilder(new string[] { })
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseConfiguration(c.Resolve<IConfiguration>());
                webBuilder.UseStartup<Startup>();
            })
            .Build();
        }).SingleInstance();
    }
}

And my question is:

Is it possible to add existing Autofac instance with all modules, configurations and databases to instance of this created WEB API without adding this things manually again on start?

Thank you

Note:

  • .NET Core console application -> loading modules from configuration
    • loaded Module 1 (Database 1) as IDatabase1
    • loaded Module 2 (Database 2) as IDatabase2
    • loaded another 40 modules
    • and loaded new module RESTModule where i will use IDatabase1, IDatabase2, and another 10 running modules
0

There are 0 answers