Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何重新加载&x27;appsettings.json';在dotnet core 3控制台应用程序中发生更改时_C#_.net Core_Configuration_Reload - Fatal编程技术网

C# 如何重新加载&x27;appsettings.json';在dotnet core 3控制台应用程序中发生更改时

C# 如何重新加载&x27;appsettings.json';在dotnet core 3控制台应用程序中发生更改时,c#,.net-core,configuration,reload,C#,.net Core,Configuration,Reload,我试图设置一个控制台应用程序来重新加载“appsettings.json”,但我无法让它对文件的更改做出反应 我已经设置了这样的主界面: private static void Main () { configuration = LoadConfiguration (); var services = ConfigureServices (); var serviceProvider = services.BuildServicePro

我试图设置一个控制台应用程序来重新加载“appsettings.json”,但我无法让它对文件的更改做出反应

我已经设置了这样的主界面:

private static void Main ()
    {
        configuration = LoadConfiguration ();

        var services = ConfigureServices ();

        var serviceProvider = services.BuildServiceProvider ();

        serviceProvider.GetService<ConsoleApp> ().Run ();
    }

    private static IConfiguration LoadConfiguration ()
        => new ConfigurationBuilder ().SetBasePath (GetApplicationDirectory ())
                                      .AddJsonFile (_APP_SETTING_FILE_NAME, optional: false, reloadOnChange: true)
                                      .Build ();

    private static IServiceCollection ConfigureServices ()
    {
        IServiceCollection services = new ServiceCollection ();

        configuration.Bind (new AppSettings ()); // binds the 'appsettings.json' to the class

        services.AddSingleton (configuration);
        services.AddTransient<ConsoleApp> ();

        services.AddOptions<AppSettings> ();  // adds the IOption<T> and IOptionMonitor<T>

        // here I'm trying to follow a post from Dino Esposito [https://www.red-gate.com/simple-talk/dotnet/net-development/asp-net-core-3-0-configuration-factsheet/][1] but services.Configure<AppSettings>() does not compile
        // the failure could be here? :-(
        services.Configure<AppSettings> (opt => opt.Default = configuration["Default"]);

        return services;
    }
任何帮助都将不胜感激

T'X
Peter

您不能使用扩展方法来执行此操作, 因为它不是为与文件配置提供程序一起工作而设计的。 为此,必须使用类中的扩展方法

关键在于课堂和课堂。 您可以在OptionMonitor类的构造函数中看到,它将获取在服务容器中注册的所有IOptionsChangeTokenSource实例,这些实例是由OptionConfigurationServiceCollectionExtensions.Configure方法注册的。IOptionsChangeTokenSource实例在配置文件更改时重新加载选项


作为比较,您可以在方法中看到,它没有注册IOptionsChangeTokenSource实例。

我最近实现了类似的东西,这绝对是一件痛苦的事情。我无法获得我所看到的各种重载更改方法。实现这一点的唯一方法是直接写入文件,然后使用新的ServiceCollection再次调用
ConfigureServices
。我很想关注这篇文章,看看其他人有什么建议。在.NET Framework中有一些与此相关的东西尚未进入.NET核心(如果可能的话)。我要补充一点,即在我实现此功能时出现了许多问题,而我上面的架构师认为此功能很容易出错,无法实际使用。您最好使用自己的配置文件,而不是appsettings。appsettings中的错误可能会导致服务处于不一致状态或完全崩溃。现在,如果加载辅助配置失败,我们仅将appsettings用作回退/默认选项。
public class ConsoleApp
{
    public AppSettings AppSettings { get; }

    public ConsoleApp (IOptionsMonitor<AppSettings> appSettingsOptionsMonitor)
    {
        AppSettings = appSettingsOptionsMonitor.CurrentValue;

        appSettingsOptionsMonitor.OnChange ((arg1, arg2) => Console.WriteLine ($"\t ---> AppSettings changed to '{arg1.Default}' - '{arg2}'"));

        Console.WriteLine ($"ctor - appsettings current value: '{AppSettings.Default}'.\n");
    }

    public void Run ()
    {
        Console.WriteLine ($"\tCurrent value: '{AppSettings.Default}' - waiting for change ...");

        while (AppSettings.Default != "Stop") { }

        Console.WriteLine ($"\t... has changed to '{AppSettings.Default}'.");
    }
}
public class AppSettings
{
    public string Default { get; set; }
}