c# - How to read connection string from data access layer in asp.net 5 -


with new asp.net 5 doing away web.config, , configurationmanagement namespace, trying figure out how read connection string data access layer project in mvc application.

researching issue, i've found says read config value project.json configuration file in startup.cs file this:

var configuration = new configuration(); configuration.addjsonfile("config.json"); configuration.addenvironmentvariables();  //////  var connstring = configuration.get("data:defaultconnection:connectionstring"); 

but don't want web project having data access. so, how data access layer project retrieve connection string in setup?

update: here's startup.cs file , configureservices method:

public void configureservices(iservicecollection services) {     // add identity services services container.     services.addidentity<applicationuser, identityrole>()         .addentityframeworkstores<applicationdbcontext>()         .adddefaulttokenproviders();      // add mvc services services container.     services.addmvc();      // register application services.     services.configure<applicationoptions>(options =>     {         options.connectionstring = "data:defaultconnection:connectionstring";     }); } 

and here's dataaccesslayer project, , repobase.cs class

public class repobase {     //private readonly string _connectionstring = configurationmanager.connectionstrings["defaultconnection"].connectionstring;     //private readonly string _connectionstring = environment.getenvironmentvariable("connectionstring");     private readonly string _connectionstring;      public repobase(ioptions<applicationoptions> appoptions)     {         this._connectionstring = appoptions.connectionstring;          // or read directly system.configuration.get("");     }      protected sqlconnection getconnection()     {         var conn = new sqlconnection(_connectionstring);         conn.openasync();          return conn;     } } 

this i'm drawing blank how retrieve applicationoptions object in dal project, or read connectionstring value set in startup.cs

configuration.addenvironmentvariables() 

method call.

update 2: oh, need use data access layer access environment variables: https://github.com/aspnet/configuration/blob/master/src/microsoft.extensions.configuration.environmentvariables/environmentvariablesconfigurationprovider.cs

this duplicate of this

on configureservices method add configuration object singleton.

public void configureservices(iservicecollection services) {     services.addsingleton(_ => configuration); } 

then update baserepo class this

public class baserepo {     private readonly iconfigurationroot config;      public baserepo(iconfigurationroot config) {         this.config = config;     }      public static sqlconnection getopenconnection() {         var cs = config.get<string>("data:defaultconnection:connectionstring");         var connection = new sqlconnection(cs);         connection.open();         return connection;     } } 

Comments