1

So I have a .net Core Web Api project and my DBAccess class library included in my solution as a separate project.

inside appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Password=L3tters&#s;Persist Security Info=True;User ID=Some_User;Initial Catalog=My_Cat;Data Source=Mt.Shasta"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

In startup.cs I have this:

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();

        }
public IConfigurationRoot Configuration { get; }    

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddMvc();

            services.Configure<IISOptions>(options =>
            {
            });

            services.AddSingleton<IConfiguration>(Configuration);    

        }    

          public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseMvc();
        }

So, in controllers I can do stuff like this:

private readonly string _connectionString;

        public FloorController(IConfiguration configuration)
        {
            _connectionString = configuration.GetConnectionString("DefaultConnection");
        }

_connectionString = configuration.GetConnectionString("DefaultConnection");

All those goodies are in SNC_GSS:

Solution Explorer Snip

But I need to get at the settings from DBAccess... I'd like to create a reference to SNC_GSS but I can't because SNC_GSS already has a reference to DBAccess and it'd create a circular ref...

Thoughts?

Methodician
  • 2,396
  • 5
  • 30
  • 49
  • Possible duplicate of [Using IConfiguration in C# Class Library](https://stackoverflow.com/questions/27880433/using-iconfiguration-in-c-sharp-class-library) – William Ardila Dec 05 '17 at 02:30
  • Is the same issue that [Using IConfiguration in C# Class Library](https://stackoverflow.com/q/27880433/1647238), see [my answer](https://stackoverflow.com/a/47645131/1647238) – William Ardila Dec 05 '17 at 02:32

2 Answers2

0

Just use something like this(constructor injection):

public class MyClass
{
   private readonly IConfiguration configuration;
   public MyClass(IConfiguration configuration)
   {
      this.configuration = configuration;
      //
      configuration.GetConnectionString("DefaultConnection");
   }
}
adem caglin
  • 22,700
  • 10
  • 58
  • 78
  • Thanks Adem but that's something I'm already doing in my main project. I added some detail to my question to show that I had that part figured out. The trouble is that in my external class library, my controllers don't seem to have access to the IConfiguration instance being created by my primary project. Any idea how to inject THAT IConfiguration into the classes in the class library my main project references? – Methodician Jul 20 '16 at 19:37
0

You can just do this:

public class AppConfigurationHelper
{
    public T GetAppSettings<T>(string key) where T : class, new()
    {
        IConfiguration config = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
            .Build();
        var appconfig = new ServiceCollection()
            .AddOptions()
            .Configure<T>(config.GetSection(key))
            .BuildServiceProvider()
            .GetService<IOptions<T>>()
            .Value;
        return appconfig;
    }
}

and then use this namespace method, e.g:

var _AppConfigurationService = new AppConfigurationHelper();
var s = _AppConfigurationService.GetAppSettings<AppSettingsModel>("AppSettings");

Of course, you need to include the project.

The file appsettings.json is that you need to config key-value.

Pang
  • 9,564
  • 146
  • 81
  • 122
FreshMan
  • 1
  • 1