1
votes

I am using Visual Studio 2017, Version 15.7.2 and working with an answer in the following link:

net core 1 (dnx 4.5.1) with enterpriselibrary 6 - setting up the connection string

public class DataConfiguration
{
    public string DefaultDatabase { get; set; }
    public List<ConnectionStringSettings> ConnectionStrings { get; set; }
}

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        //Get the Database Connections from appsettings.json
        DataConfig = configuration.Get<DataConfiguration>(); 

        var defaultDb = DataConfig.ConnectionStrings?.Find(c => c.Name == DataConfig.DefaultDatabase);
        DatabaseFactory.SetDatabases(() => new SqlDatabase(defaultDb.ConnectionString), GetDatabase);

        Configuration = configuration;
    }

    public Database GetDatabase(string name)
    {
        var dbInfo = DataConfig.ConnectionStrings.Find(c => c.Name == name);

        if (dbInfo.ProviderName == "System.Data.SqlClient")
        {
            return new SqlDatabase(dbInfo.ConnectionString);
        }

        return new MySqlDatabase(dbInfo.ConnectionString);
    }

The part I cannot get to work is:

//Get the Database Connections from appsettings.json
DataConfig = configuration.Get<DataConfiguration>(); 

configuration.Get<DataConfiguration>() returns an empty instance of DataConfiguration. I expected it to populate or bind to DataConfiguration in the appsettings file.

My appsettings.json:

{
    "DataConfiguration": {
        "DefaultDatabase": "MyDB",
        "ConnectionString": "Server=MyServer;Database=MyDB;Integrated Security=True;MultipleActiveResultSets=true",
        "ProviderName":  "System.Data.SqlClient"
    },
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Warning"
        }
    }
}

My Startup.cs

public class DataConfiguration
    {
        public string DefaultDatabase { get; set; }
        public string ConnectionString { get; set; }
        public string ProviderName { get; set; }
    }
    public class Startup
    {
        public DataConfiguration DataConfig;
        public Startup(IConfiguration configuration)
        {
            DataConfig = configuration.Get<DataConfiguration>();
            DatabaseFactory.SetDatabases(() => new SqlDatabase(DataConfig.ConnectionString), GetDatabase);

            Configuration = configuration;
        }

        public Database GetDatabase(string name)
        {
            if(DataConfig.ProviderName == "System.Data.SqlClient")
            {
                return new SqlDatabase(DataConfig.ConnectionString);
            }
            return null;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            var config = new DataConfiguration();
            Configuration.Bind("DataConfiguration", config);
            services.AddMvc();
            services.AddSingleton(config);
            //services.AddSingleton<IConfiguration>(Configuration);
            services.AddTransient<IPatientRepository, PatientRepository>();
        }

What am I missing?

1

1 Answers

0
votes

So, if you want use settings file this way, you have to write:

var DataConfig = new DataConfiguration();
Configuration.GetSection(nameof(DataConfiguration)).Bind(DataConfig);

But usually settings and configurations uses another way: https://docs.microsoft.com/aspnet/core/fundamentals/startup?view=aspnetcore-2.0

or even easier:

services.Configure<DataConfiguration>configuration.GetSection(nameof(DataConfiguration)))
services.AddScoped(serviceProvider =>
        {
            var options = serviceProvider.GetRequiredService<IOptions<DataConfiguration>>();
            return new SqlDatabase(options.Value.ConnectionString);
        });

I hope its clear, but maybe final example should be slightly corrected :)