简体   繁体   English

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

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

I am migrating an Asp.Net MVC application to.Net5.我正在将 Asp.Net MVC 应用程序迁移到.Net5。 I had a static class that served as a façade for the settings in web.config.我有一个 static class 作为 web.config 中设置的外观。 My Setting class exposed static properties, each one of a class representing settings groups, for example:我的 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
    {
        // ...
    }
}

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

Is there any way to map all my settings to one single object, for example, for appsettings.json:有什么方法可以将 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"
    }
  }
}

Classes:课程:

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; }
}

Configuration (in Startup.cs):配置(在 Startup.cs 中):

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

(Yes, I know I can do services.Configure<MainDB>(Configuration.GetSection("Settings:MainDB")); and services.Configure<ExternalApi>(Configuration.GetSection("Settings:ExternalApi")); but I'd like to get all the settings in one sigle object if it is possible. (是的,我知道我可以做services.Configure<MainDB>(Configuration.GetSection("Settings:MainDB"));services.Configure<ExternalApi>(Configuration.GetSection("Settings:ExternalApi"));但我'如果可能的话,我想在一个单一的 object 中获得所有设置。

Any suggestion?有什么建议吗?

Am assuming here you're talking about binding (app settings file to a single object?) If you're okay with bit of extra code then this could work for what you want to achieve.我假设您在这里谈论的是绑定(应用程序设置文件到单个 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; }
}

If you're only trying to setup a single node/section's hierarchy you can simply do a ((IConfiguration)config.GetSection("SectionName")).Bind(myObject)如果您只想设置单个节点/部分的层次结构,您可以简单地执行((IConfiguration)config.GetSection("SectionName")).Bind(myObject)

Either way config.Bind(object) is the magic bit here.无论哪种方式config.Bind(object)都是这里的魔力。

IConfiguration Configuration is your facade to the appsettings (actually, to all settings, whether they are coming from appsettings, user secrets, or wherever). IConfiguration Configuration您对appsettings的外观(实际上,对于所有设置,无论它们来自 appsettings、用户机密还是其他地方)。 You can use您可以使用

var configurationSection = Configuration.GetSection("Settings") 

to get to specific section, and then maybe have this as private static variable in MySettings class - but it seems redundant.到达特定部分,然后可能将其作为MySettings class 中的私有 static 变量 - 但它似乎是多余的。 And having internal class for something that can be easily retrieved as Configuration[key] seems double-redundant并且具有内部 class 可以轻松检索为Configuration[key]的东西似乎是双重冗余的

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

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