13

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.

FerX32
  • 1,407
  • 3
  • 18
  • 28
  • look at this, it might help you: https://stackoverflow.com/questions/37568084/asp-net-core-accessing-appsettings-from-another-project-in-solution – Barr J Jan 02 '18 at 05:54

2 Answers2

23

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.

FerX32
  • 1,407
  • 3
  • 18
  • 28
18

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");
Unheilig
  • 16,196
  • 193
  • 68
  • 98
DeanC
  • 1,249
  • 1
  • 7
  • 3