ASP.NET 核心 2.0 配置绑定



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

网络核心 1 (DNX 4.5.1( 与企业库 6 - 设置连接字符串

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>()返回数据配置的空实例。 我希望它在应用程序设置文件中填充或绑定到数据配置。

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

我的创业公司.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>();
}

我错过了什么?

因此,如果要以这种方式使用设置文件,则必须编写:

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

但通常设置和配置使用另一种方式: https://learn.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);
});

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

相关内容

  • 没有找到相关文章

最新更新