簡體   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