Configuration.GetSection 从 appsetting.json 获取值,但 Configuration.GetSection。始终绑定 returns null

Configuration.GetSection gets value from appsetting.json but Configuration.GetSection.Bind always returns null

我在下面的代码中试图将我的 appsettings.json 与我的变量绑定,我的变量是 class 类型,其模型已根据 JSON 模式适当定义。

在调试期间,在快速观察中,我能够在 config.GetSection("TableStorageRule") 中看到我的 appsettings.json 的值,但对于 config.GetSection("TableStorageRule").Bind(tableStorageOutput) 它的 .

var builder = new ConfigurationBuilder()
        .SetBasePath(Path.Combine(Root))
        .AddJsonFile("appsettings.json", optional: false);

var config = builder.Build();

var tableStorageOutput = new TableStorageRule();            
config.GetSection("TableStorageRule").Bind(tableStorageOutput);
var nameOfFilter = tableStorageOutput.Name;

我想知道我做错了什么?

这是我的模型class定义

public class TableStoreSettings
{
    public class AzureTableSettings
    {
        public string Account { get; set; }
        public string Key { get; set; }
        public string Table { get; set; }
    }

    public AzureTableSettings AzureTable { get; set; }

    public Uri SchemaBaseUri { get; set; }
}

public class TableStorageRule
{
    public string Name { get; set; }
    public TwisterDataFilter DataFilter { get; set; }
    public TableStoreSettings TableSettings { get; set; }
}

这是我的 Json 架构>

{  "TableStorageRule": [
  {
    "Name": "filterRule1",
    "DataFilter": {
      "DataSetType": "Settings1"
    
    },
    "TableStoreSettings": {
      "AzureTable": {
        "Account": "account1",
        "Table": "table1",
        "Key": "key1"
      },
      "SchemaBaseUri": "https://test.web.core.windows.net/"
    }
  } 
]}

问题出在您的 Json 上。 TableStoreSettings 需要重命名为 TableSettings 以匹配 class,并且您的 TableStorageRule 不是规则数组。

{
  "TableStorageRule": {
    "Name": "filterRule1",
    "DataFilter": {
      "DataSetType": "Settings1"

    },
    "TableSettings": {
      "AzureTable": {
        "Account": "account1",
        "Table": "table1",
        "Key": "key1"
      },
      "SchemaBaseUri": "https://test.web.core.windows.net/"
    }
  }
  
}

如果您打算制定一系列规则,我建议再添加一个 Top Level class.

    public class TableStorageRules
    {
        public List<TableStorageRule> Rules { get; set; }
    }

那么你的Json会是这样

{
  "TableStorageRule": {
    "Rules": [
      
      {
        "Name": "filterRule1",
        "DataFilter":
        {
          "DataSetType": "Settings1"

        },
        "TableSettings":
        {
          "AzureTable": {
            "Account": "account1",
            "Table": "table1",
            "Key": "key1"
          },
          "SchemaBaseUri": "https://test.web.core.windows.net/"
        }

      }
    ]
  }

}

Bind 你会用这个

        var builder = new ConfigurationBuilder()
                        .SetBasePath(Path.Combine(Root))
                        .AddJsonFile("appsettings.json", optional: false);

        var config = builder.Build();

        var tableStorageOutput = new TableStorageRules();
        config.GetSection("TableStorageRule").Bind(tableStorageOutput);
        var nameOfFilter = tableStorageOutput.Rules[0].Name;