[英]ASP.Net Core live update configuration on strongly typed
I was trying out ASP.Net Core and I and i was fooling around with the new configuration implementation. 我正在尝试ASP.Net Core,并且我和新配置实现混在一起。 I created my configuration like this: 我创建了这样的配置:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddUserSecrets();
ConfigurationManager.SetConfiguration(builder.Build());
}
I have my own ConfigurationManager class which basically holds the IConfigurationRoot and i have some helper methods in it. 我有我自己的ConfigurationManager类,该类基本上包含IConfigurationRoot,并且其中包含一些辅助方法。
public static class ConfigurationManager
{
private static IConfigurationRoot _configuration { get; set; }
public static IConfigurationRoot GetConfiguration()
{
return _configuration;
}
public static void SetConfiguration(IConfigurationRoot configuration)
{
_configuration = configuration;
}
public static string GetString(string appSettingKey)
{
return _configuration.GetValue<string>(appSettingKey);
}
public static IConfigurationSection GetSection(string sectionKey)
{
return _configuration.GetSection(sectionKey);
}
}
I can access my 'Foo' setting like this: 我可以这样访问“ Foo”设置:
var foo = ConfigurationManager.GetString("Foo");
When I run my application the code above will give me the value of 'Foo', which is "Bar". 当我运行我的应用程序时,上面的代码将为我提供'Foo'的值,即“ Bar”。 If I change the value of 'Foo' in my applicationsettings.json from "Bar" to "BarBar" while my application is still running and come across the code above it will give me the new value of 'Foo'. 如果在我的应用程序仍在运行时将applicationsettings.json中的'Foo'值从“ Bar”更改为“ BarBar”,并且遇到上面的代码,它将为我提供'Foo'的新值。
I have created a Configuration class so I can have my settings as strongly typed. 我创建了一个Configuration类,因此可以将我的设置设为强类型。
public class Configuration
{
public string Foo { get; set; }
}
I Bind it to my configuration like this: 我将其绑定到我的配置,如下所示:
services.Configure<Configuration>(ConfigurationManager.GetConfiguration());
I can acces the 'Foo' setting through dependency injection in my controller like this: 我可以通过在控制器中进行依赖注入来访问“ Foo”设置,如下所示:
public MyController(IOptions<Configuration> config)
{
var foo = config.Value.Foo;
}
When I call the controller the value of 'Foo' is "Bar". 当我调用控制器时,“ Foo”的值为“ Bar”。 When I change the value of 'Foo' into "BarBar" and call the controller again the value of 'Foo' is still "Bar" in my strongly typed configuration. 当我将“ Foo”的值更改为“ BarBar”并再次调用控制器时,在我的强类型配置中,“ Foo”的值仍为“ Bar”。
Is there a way to make the strongly typed class change when I change my applicationsettings.json on the fly as it does when I don't use the strongly typed class? 当我快速更改我的applicationsettings.json时,是否有办法像不使用强类型的类时那样更改强类型的类?
If you are using ASP.NET Core 1.1, you can get updates of strongly typed configuration with 如果您使用的是ASP.NET Core 1.1,则可以通过以下方式获取强类型配置的更新:
IOptionsSnapshot IOptionsSnapshot
where T is you strongly typed configuration class. 其中T是您强类型配置类。
You can find working example here and read more about it here in the IOptionsSnapshot section 您可以在此处找到工作示例,并在IOptionsSnapshot部分中阅读有关此示例的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.