1

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
Jara
  • 11
  • 2
  • Does this answer your question? [Autofac: How to load assemblies that are referenced but not directly used](https://stackoverflow.com/questions/33811015/autofac-how-to-load-assemblies-that-are-referenced-but-not-directly-used) – Cyril Durand Apr 08 '20 at 13:49
  • RESTModule must consume other running modules. RESTModule will be provide data from database or data from GPIOModule or from other modules. And i must be able set data to this modules from RESTModule. So i need add all neccesary running modules to the RESTModule and use it in API controllers. – Jara Apr 09 '20 at 09:34
  • It's still unclear to me. Could you share how you will do it manually ? and what you tried to do it automatically ? – Cyril Durand Apr 09 '20 at 09:41
  • On start RESTModule, in method Load(ContainerBuilder builder) i will resolve GPIO module with "var gpioModule = c.Resolve();" and i will add it to new API instance with: .UseServiceProviderFactory(new AutofacServiceProviderFactory(action => { action.RegisterInstance(gpioModule); })). On GET request from client, i will resolve IGPIO module in GPIOController and i will provide actual data from this module. I need to use existing autofac container in created API (RESTModule is creating this API) with all his modules and work with this modules in API controllers. – Jara Apr 09 '20 at 10:29
  • I think, i found very relevant topic [here](https://github.com/autofac/Autofac.Extensions.DependencyInjection/issues/26) – Jara Apr 09 '20 at 11:08

0 Answers0