如何在 config json 文件中嵌套配置并使用 bit 读取它?

How to have nested configurations in config json file and read it using bit?

到目前为止,我已经看到一些示例代码,其中一些配置被插入到名为 environment.json 的文件中,如下所示:

[
    {
        "Name": "Default",

    "AppInfo": {
          "Name": "blahblah",
          "Version": "1"
    },
    "Configs": [
      {
        "Key": "someconfiguration",
        "Value": "some value"
      },
      {
        "Key": "another configuration ",
        "Value": "blah blah"
      },
    ]
   }
]

然后在需要的时候,可以像这样从配置文件中读取数据:

var value = DefaultAppEnvironmentsProvider.Current
            .GetActiveAppEnvironment()
            .GetConfig<string>("SomeConfiguration");

问题是:

如果我想要一些 configuration 的值是嵌套的 json listjson object 怎么办?我想要这样的东西:

"key": "Address",
"value": {
    "street": "some street name",
    "postal code": "blah blah",
    ...
}

如何使用 bit 读取此类配置?

提前感谢您抽出时间。

首先,创建一个 class 来定义您的配置契约:

public class MailServerConfig
{
    public Uri ServerUrl { get; set; }

    public string AnotherConfig { get; set; }
}

然后将以下内容添加到您的 environments.json 文件中:

,
      {
        "Key": "MailServerConfig",
        "Value": {
          "$type": "SampleApp.Configurations.MailServerConfig, SampleApp",
          "ServerUrl": "https://google.com/",
          "AnotherConfig": "!"
        }
      }

在你的控制器中(或者任何你想读取配置的地方),注入 AppEnv

public AppEnvironment AppEnv { get; set; }

然后阅读您的配置如下:

MailServerConfig mailServerConfig = AppEnv.GetConfig<MailServerConfig>("MailServerConfig");

请注意,在 environments.json 中,$type 是必需的,它应该是嵌套 json 对象的第一行。