1
votes

With the new ASP.NET 5 doing away with web.config, and thus the ConfigurationManagement namespace, I was trying to figure out how to read the connection string from my data access layer project in my MVC application.

Researching this issue, everything I've found says to just read the config value from the project.json configuration file in the Startup.cs file like this:

var configuration = new Configuration();
configuration.AddJsonFile("config.json");
configuration.AddEnvironmentVariables();

//////

var connString = configuration.Get("Data:DefaultConnection:ConnectionString");

But I don't want my web project having anything to do with the data access. So, how does my data access layer project retrieve the connection string in this setup?

Update: Here's my startup.cs file and the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    // Add Identity services to the services container.
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    // Add MVC services to the services container.
    services.AddMvc();

    // Register application services.
    services.Configure<ApplicationOptions>(options =>
    {
        options.ConnectionString = "Data:DefaultConnection:ConnectionString";
    });
}

And here's my DataAccessLayer project, and my 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 just read directly from System.Configuration.Get("");
    }

    protected SqlConnection GetConnection()
    {
        var conn = new SqlConnection(_connectionString);
        conn.OpenAsync();

        return conn;
    }
}

This is where I'm drawing a blank as to how to retrieve my ApplicationOptions object in my DAL project, or just read the connectionstring value that is set in the Startup.cs

configuration.AddEnvironmentVariables()

method call.

Update 2: Oh, is this what I need to use from my Data Access Layer to access the environment variables: https://github.com/aspnet/configuration/blob/master/src/Microsoft.Extensions.Configuration.EnvironmentVariables/EnvironmentVariablesConfigurationProvider.cs

1
If the DA layer is a DLL, it should be able to read the project.json file of the consuming application. If it's its own application, it would have its own project.json file. Unless there's a part of the equation I'm missing? - Tim
There's nothing wrong with having your web project retrieve the connection string and pass it to the data layer. Or the web project can pass its entire configuration to the data layer and then the data layer can pick and choose what it wants out of the configuration. - mason
@Tim it's just a separate DLL, like you said. - ganders
@mason what namespace would be used for that since ConfigurationManager doesn't work anymore? - ganders
ASP.NET 5 is dead : hanselman.com/blog/… Sorry for your loss - Mark Schultheiss

1 Answers

2
votes

This is a duplicate of this

On your ConfigureServices method add your configuration object as a singleton.

public void ConfigureServices(IServiceCollection services) {
    services.AddSingleton(_ => configuration);
}

then update your BaseRepo class like 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;
    }
}