簡體   English   中英

如何將 appsettings.json 設置與設置名稱中的點綁定

[英]How to bind appsettings.json setting with dots in the setting name

After watching/reading some tutorials about the option pattern in .net for binding your settings to a c# model, I had this particular question about binding the setting “Microsoft.Hosting.Lifetime” to a model.

在此處輸入圖像描述

情況我知道如何通過選項模式將設置綁定到 model。 所以擁有 appsettings.json 文件,如

"FeatureFlags": {
    "IsFeature1Enabled": "false",
    "IsFeature2Enabled": "false",
    "IsFeature3Enabled": "true"
  }

會產生如下代碼:

model:

public class FeatureFlagOptions
{
    public bool IsFeature1Enabled { get; set; } = false;
    public bool IsFeature2Enabled { get; set; } = false;
    public bool IsFeature3Enabled { get; set; } = false;
}

啟動.cs:

private void SetUpAppSettingsModels(IServiceCollection services)
{
    services.Configure<FeatureFlagOptions>(options => Configuration.GetSection("FeatureFlags").Bind(options));
}

class 帶 DI:

private readonly FeatureFlagOptions _featureFlags;
public ClassNameHere(IOptions<FeatureFlagOptions> featureFlags)
{
    _featureFlags = featureFlags.Value;
}

問題

如果您啟動一個新的 web 應用程序並選擇例如 web api,則標准應用設置將包含應用設置“Microsoft.Hosting.Lifetime”。 就像一開始的圖片一樣。 那么我應該如何在我的 model 中實現該設置?

我在 Google 上搜索了很多,在 stackoverflow 甚至我最喜歡的博客作者 Rick Strahl 上進行了搜索( https://weblog.west-wind.com/posts/2017/dec/12/easy-configuration-binding-in-aspnet-core-revisited ) 跳過了記錄部分的部分。 我希望有人可以幫助我解決這個問題。 謝謝!

為什么我想要那個屬性?:因為原因;)

感謝 Asawyer,我們找到了解決方案。 也許有更好的,但現在我們將其修復如下:

Model:

public class LoggingModel
{
    public LogLevel LogLevel { get; set; }
}

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

    //[JsonProperty("Microsoft.Hosting.Lifetime")] => Did not work :(
    public string MicrosoftHostingLifetime { get; set; }
}

啟動.cs:

services.Configure<LoggingModel>(options => Configuration.GetSection("Logging").Bind(options));
services.Configure<LoggingModel>(options =>
{
     options.LogLevel.MicrosoftHostingLifetime = Configuration["Logging:LogLevel:Microsoft.Hosting.Lifetime"];
});

class 帶 DI:

private readonly LoggingModel _loggingModel;
public ClassNameHere(IOptions<LoggingModel> loggingOptions)
{
    _loggingModel= loggingOptions.Value;
}

在 .NET 6 中,添加了ConfigurationKeyNameAttribute來支持這個

在類似的情況下,我使用了一個不那么強類型的解決方案,因為閱讀 Startup.cs 中的特定設置的想法並不像一個干凈的解決方案。

這是我的 appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

為了閱讀“Logging.LogLevel”部分,我使用了 Dictionary

public class Logging
{
    public const string Default = "Default";
    public const string Microsoft = "Microsoft";
    public const string Microsoft_Hosting_Lifetime = "Microsoft.Hosting.Lifetime";

    public Dictionary<string, string> LogLevel { get; set; }
}

並在假裝它的鍵不是自由樣式字符串的同時使用這個字典<string, string>

var logLevels = loggingOptions.Value.LogLevel;
var logLevels_Default = logLevels[Logging.Default];
var logLevels_Microsoft = logLevels[Logging.Microsoft];
var logLevels_Microsoft_Hosting_Lifetime = logLevels[Logging.Microsoft_Hosting_Lifetime];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM