更新后无法读取应用程序设置

Can't read appsettings after update

应用程序的工作原理:

选项 1 - 从应用程序设置加载项目 选项 2 - 包括一个新项目

如果select "Option 1",显示itens。这行得通。 所以...我使用 "Option 2" 添加了一个新项目。这行得通。 但是如果我select"Option 1"显示itens,列表是空的。

我的应用设置

  "app": {
    "hosts": [
      {
        "ip": "127.0.0.1",
        "hostname": "NotFound"
      },
      {
        "ip": "127.0.0.1",
        "hostname": "MyProject"
      },
      {
        "ip": "127.0.0.1",
        "hostname": "AnotherProject.dev"
      }
    ]
  }
}

绑定

AppSettingsModel appSettings = new AppSettingsModel();
Configuration.Bind("app", appSettings);

我将应用设置配置为 reloadOnChange

static IConfiguration LoadConfiguration() => 
            new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", false, true)
                .Build();

更新应用设置

var appSettings = new AppSettingsModel();
                        var appSettings = new AppSettingsModel();
Configuration.Bind("app", appSettings);

appSettings.Hosts.Add(new HostsModel()
{ 
    Ip = "102.54.94.97",
    Hostname = "rhino.acme.com" 
});

var jsonSerializerOptions = new JsonSerializerOptions()
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

var resultC = JsonSerializer.Serialize(appSettings, jsonSerializerOptions);

File.WriteAllText("appsettings.json", resultC);

我的代码 -> https://github.com/tiago-aguiar-moreira/ManagerHosts

我在 Bind 方法中输入了一个部分,但在我的情况下没有必要。

之前

Configuration.Bind("hosts", appSettings);

之后

Configuration.Bind(appSettings);