Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net Core: access to appsettings.json values from Autofac Module

AspNet core app

1) Autofac module like that

public class AutofacModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
      //Register my components. Need to access to appsettings.json values from here
    }
}

2) Module from step№1 registered into Startup.cs

public void ConfigureContainer(ContainerBuilder builder)
    {
       builder.RegisterModule(new AutofacModule());
    }

How to access to appsettings.json values from AutofacModule? I need that for create my objects inside AutofacModule and using it for DI.

like image 385
Frank59 Avatar asked Oct 23 '17 11:10

Frank59


1 Answers

Need to change step №2

        public void ConfigureContainer(ContainerBuilder builder)
    {
        //get settigns as object from config
        var someSettings= Configuration.GetSection(typeof(SomeSettings).Name).Get<SomeSettings>();                                                    
        //put settings into module constructor 
        builder.RegisterModule(new AutofacModule(someSettings));
    }

I don't know is "best practise" way or not, but it works.

like image 78
Frank59 Avatar answered Oct 06 '22 10:10

Frank59