在 netcoreapp1.1 中,我的配置是在 中定义的Startup
,其中包含:
public Startup(IHostingEnvironment env)
{
_configuration =
new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.Build();
}
private readonly IConfigurationRoot _configuration;
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<Stuff1>(_configuration.GetSection("stuff1"));
services.Configure<Stuff2>(_configuration.GetSection("stuff2"));
services.AddDbContext<Context>(options =>
options.UseSqlite(_configuration.GetConnectionString("DefaultConnection")),
);
}
但在 netcoreapp2.0 中,配置是在Program
. 所以Startup
现在只有这个:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var configuration = //??? how do I get this?
services.AddOptions();
services.Configure<Stuff1>(configuration.GetSection("stuff1"));
services.Configure<Stuff2>(configuration.GetSection("stuff2"));
services.AddDbContext<Context>(options =>
options.UseSqlite(configuration.GetConnectionString("DefaultConnection")),
);
}
那么如何获取IConfigurationRoot
in ConfigureServices()
,以便设置强类型Stuff1
和Stuff2
选项以及连接字符串等其他内容?