Search code examples
c#asp.net-mvcautofacn-tier-architectureautofac-module

How to make the connection string available in a N-Tier ASP.NET MVC Core 1.0 App with Autofac


I wired the layer together via autofac modules. Big thanks to endeffects. Here is the HowTo. Now I'm trying to make the connection string available in the DAL layer. I tried to register the:

Configuration (Microsoft.Extensions.Configuration)

From the Startup class but without any success.


Solution

  • I have the following line in my ConfigureServices method in startup.cs:

    services.AddSingleton(serviceType => Configuration);
    services.AddInstance<Microsoft.Extensions.Configuration.IConfiguration>(Configuration);
    
    builder.Populate(services);
    var container = builder.Build();
    var serviceProvider = container.Resolve<IServiceProvider>();
    return serviceProvider;
    

    Then in my DAL, I access that via constructor injections like:

    public MyDataContext(IConfiguration configurations)
    

    I can then access that item and pull my connection info like:

    configurations["Data:MyConnection:ConnectionString"]