Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Configuration/Settings on static class - Asp Core

I have 3 solutions. Project.Web, Project.Core (Business), and Project.Layer(Models).

In Project.Core, I have a static file that I can call like this Business.GetAllData(); from Project.Web.Controller.

This calls DAL/EF files and gets data(BusinessDal.GetData()).

        using (DBContext db = new DBContext())
        {
            return db.GetAllData().ToPOCO();
        }

On my configuration/DbContext.cs, I have this:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    #if DEBUG
        optionsBuilder.UseSqlServer(@"connstring");
    #else
        optionsBuilder.UseSqlServer(@"connstring");
    #endif
    // How do I access configuration here? Configuration["ConnectionString"]
}

What I'm trying to do is read settings from my appsettings.json. I made sure settings are loaded properly on startup.cs

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

But now what?... MS Document shows how to read it from controllers. And that part works fine, I can read my settings on Controllers. However, I am not sure how to pass it to another project and still be able to call it from a static class.

like image 320
FerX32 Avatar asked Jan 02 '18 05:01

FerX32


2 Answers

A slightly shorter version based on the same principle as above:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    StaticConfig = configuration;
}
public static IConfiguration StaticConfig { get; private set; }

To use in another static class:

string connString = Startup.StaticConfig.GetConnectionString("DefaultConnection");
like image 80
DeanC Avatar answered Sep 21 '22 01:09

DeanC


I feel like this may be more work than necessary, but I'm in a rush so this is what I'm going with so far. Feel free to post other solutions as they become available.

I create another static class AppSettingsProvider.cs

public static class AppSettingsProvider
{
    public static string DbConnectionString { get; set; }
    public static bool IsDevelopment { get; set; }
}

Then I set them on my Startup.cs

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();

    BuildAppSettingsProvider();
}
private void BuildAppSettingsProvider()
{
    AppSettingsProvider.ConnectionString = Configuration.GetConnectionString("DBContext");
    AppSettingsProvider.IsDevelopment = Configuration["IsDev"];
}

Then I can call it from my DbContext.cs

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    string connString = AppSettingsProvider.ConnectionString;
}

P.S. I tried the dependency injection method into DbContext (by having contructors). However, that did not work for me because I was calling DbContext from a static file, so the DbContextOptions was getting lost.

like image 36
FerX32 Avatar answered Sep 17 '22 01:09

FerX32