在没有DI的控制台应用程序中将IConfigurationSection与可刷新配置绑定

时间:2018-10-29 20:27:12

标签: c# .net-core .net-standard

我在使.Net Core Console应用程序中的输入配置可刷新时遇到问题。

该应用将从配置中加载键入的对象...

{
     "LibrarySettings": {
       "SettingA": "ValueA",
       "SettingB": "ValueB"
     }
}

...并将它们作为IOption<SomeLibrarySettings>传递给库

public class LibrarySettings
{
    public string SettingA { get; set; }
    public string SettingB { get; set; }
}

public class BusinessLogic
{
    private IOptions<LibrarySettings> _settings;

    public BusinessLogic(IOptions<LibrarySettings> settings)
    {
        _settings = settings;
    }

    public string GetValueA => _settings.Value.SettingA;

    public string GetValueB => _settings.Value.SettingB;
}

在运行时,如果我更改appsettings.json,我将期望基础IOptions实例相应地更新值。

public class Program
{
    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", false, true);

        var configuration = builder.Build();

        LibrarySettings settings = new LibrarySettings();

        configuration.Bind("LibrarySettings", settings);

        BusinessLogic logic = new BusinessLogic(Options.Create(settings));

        while (true)
        {
            configuration.Reload();
            Console.WriteLine();
            Console.WriteLine(logic.GetValueA);
            Console.WriteLine(logic.GetValueB);
            Console.ReadLine();
        }
    }
}

不幸的是事实并非如此。当我启动此代码并更改复制的appsettings.json中的值时,不会重新加载应用程序中的值。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

使用由IOptionsSnapshot插入的IOptions

public class BusinessLogic
{
     private IOptionsSnapshot<LibrarySettings> _settings;

     public BusinessLogic(IOptionsSnapshot<LibrarySettings> settings)
     {
         _settings = settings;
     }

     public string GetValueA => _settings.Value.SettingA;

     public string GetValueB => _settings.Value.SettingB;
 }

ref Options patterns in ASP.NET Core

编辑:避免常见错误('T' must be a non-abstract type with a public parameterless constructor

public class LibrarySettings
{
    public string SettingA { get; set; }
    public string SettingB { get; set; }
    public LibrarySettings() {}
}

使其正常工作:

class Program
    {

        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", false, true);

            var configuration = builder.Build();

            var settings = new LibrarySettings();

            configuration.GetSection("LibrarySettings").Bind(settings);

            var x = settings.SettingA;
        }

    }

    public class BusinessLogic
    {
        private LibrarySettings _settings;

        public BusinessLogic(IOptionsSnapshot<LibrarySettings> settings)
        {
            _settings = settings.Value;
        }

        public string GetValueA => _settings.SettingA;

        public string GetValueB => _settings.SettingB;
    }
    public class LibrarySettings
    {
        public string SettingA { get; set; }
        public string SettingB { get; set; }
        public LibrarySettings() { }
    }