ASP.NET Core 2.0配置绑定

时间:2018-05-30 03:15:12

标签: asp.net-core-2.0 enterprise-library-6

我正在使用Visual Studio 2017版本15.7.2并使用以下链接中的答案:

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

我无法工作的部分是:

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

configuration.Get<DataConfiguration>()返回DataConfiguration的空实例。我希望它能够在appsettings文件中填充或绑定到DataConfiguration。

我的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"
        }
    }
}

我的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>();
        }

我错过了什么?

1 个答案:

答案 0 :(得分:0)

所以,如果你想以这种方式使用设置文件,你必须写:

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

但通常设置和配置使用另一种方式: https://docs.microsoft.com/aspnet/core/fundamentals/startup?view=aspnetcore-2.0

甚至更容易:

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

我希望它的明确,但也许最后的例子应该稍微纠正一下:)