使用.NET-Core应用程序,configuration data read from some file/environment将加载到IConfigurationRoot
实例中。但我不断看到示例表明注入的实例是IOptions<T>
..而不是IConfigurationRoot
。
IOptions
? AppSettings
POCO? 例如......
public static IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
.AddJsonFile("appSettings.json", true, true)
.AddJsonFile($"appSettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// Register the IConfiguration instance which MyOptions binds against.
services.Configure<AppConfiguration>(Configuration);
// <snip other stuff>
}
public AccountController(IOptions<AppConfiguration> appConfiguration)
{
// do stuff with appConfiguration here.
}