繁体   English   中英

如何从 appsettings.json in.Net 5 (Asp.Net) 中 map 多级设置

[英]How to map multiple levels of Settings from appsettings.json in .Net 5 (Asp.Net)

我正在将 Asp.Net MVC 应用程序迁移到.Net5。 我有一个 static class 作为 web.config 中设置的外观。 我的 Setting class 暴露了 static 属性,每一个 class 代表设置组,例如:

public static class MySettings
{
    public static class MainDB
    {
        public static string ConnectionString
        {
            get
            {
                // Code to retrieve the actual values
                return "";
            }
        }
    }

    public static class ExternalApi
    {
        public static string Uri
        {
            get
            { // Code to retrieve the actual values
                return "";
            }
        }
    }

    public static class OtherSettings
    {
        // ...
    }
}

在.Net Core 5(实际上,从.Net Core 2 开始)我们使用 POCO 的 tyo 读取设置,如https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore 中所述-5.0

有什么方法可以将 map 我的所有设置设置为一个 object,例如,对于 appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Settings": {
    "MainDB": {
      "ConnectionString": "whatever needed to access the db"
    },
    "ExtrenaAPI" {
      "Uri": "https://whatever.api.com",
      "Key": "mysupersecretkey",
      "Secret": "mysupersecret-uh-secret"
    }
  }
}

课程:

public class MainDB
{
    public string ConnectionString { get; set; }
}

public class ExternalApi
{
    public string Uri { get; set; }
    public string Key { get; set; }
    public string Secret { get; set; }
}

public class Settings
{
    public MainDB MainDB { get; set; }

    `public ExternalApi ExternalApi { get; set; }
}

配置(在 Startup.cs 中):

services.Configure<Settings>(Configuration.GetSection("Settings"));

(是的,我知道我可以做services.Configure<MainDB>(Configuration.GetSection("Settings:MainDB"));services.Configure<ExternalApi>(Configuration.GetSection("Settings:ExternalApi"));但我'如果可能的话,我想在一个单一的 object 中获得所有设置。

有什么建议吗?

我假设您在这里谈论的是绑定(应用程序设置文件到单个 object?)如果您对一些额外的代码没问题,那么这可能适用于您想要实现的目标。

IConfigurationRoot configRoot = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
Settings settingsRoot = new Settings(); // custom POCO with appsettings structure
((IConfiguration)configRoot).Bind(settingsRoot);

public class Settings
{
    public Logging Logging { get; set; }

    public string AllowedHosts { get; set; }
}
public class LogLevel
{
    public string Default { get; set; }
    public string Microsoft { get; set; }
}

如果您只想设置单个节点/部分的层次结构,您可以简单地执行((IConfiguration)config.GetSection("SectionName")).Bind(myObject)

无论哪种方式config.Bind(object)都是这里的魔力。

IConfiguration Configuration您对appsettings的外观(实际上,对于所有设置,无论它们来自 appsettings、用户机密还是其他地方)。 您可以使用

var configurationSection = Configuration.GetSection("Settings") 

到达特定部分,然后可能将其作为MySettings class 中的私有 static 变量 - 但它似乎是多余的。 并且具有内部 class 可以轻松检索为Configuration[key]的东西似乎是双重冗余的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM