简体   繁体   English

强类型的ASP.Net Core实时更新配置

[英]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.

相关问题 如何在ASP.NET Core中设置强类型配置? - How to set up strongly typed configuration in ASP.NET Core? 在 ASP.net 核心应用程序的强类型配置类中使用 System.Uri - Use System.Uri in strongly typed configuration class for ASP.net core app 是否可以将强类型数组绑定为ASP.NET Core中的配置模型? - Is it possible to bind strongly typed arrays as configuration models in ASP.NET Core? 无法使用 Asp.Net Core 3 中的命名选项读取/绑定具有强类型 class 的配置设置 - Unable to read/bind configuration settings with strongly-typed class using Named Options in Asp.Net Core 3 ASP.NET 5:如何在更改时重新加载强类型配置 - ASP.NET 5: How to reload strongly typed configuration on change .net 核心控制台应用程序强类型配置 - .net core console app strongly typed configuration .net core控制台应用程序强类型配置 - .net core Console application strongly typed Configuration 是否可以直接在ASP.NET 5(vNext)的类库中访问强类型的配置设置? - Accessing strongly typed configuration settings directly into class library in ASP.NET 5 (vNext)? 如何在asp.net core中以强类型方式获取资源字符串? - How to get resource strings in strongly typed way in asp.net core? ASP.Net Core MVC 如何针对强类型模型发布未知数量的字段 - ASP.Net Core MVC how to post unknown number of fields against strongly-typed model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM