dependency-injection.net-coreautofacautofac-module

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.


Solution

  • 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.