简体   繁体   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. 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.

在此处输入图像描述

Situation I know how to bind settings to a model via the option pattern.情况我知道如何通过选项模式将设置绑定到 model。 So having the appsettings.json file like所以拥有 appsettings.json 文件,如

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

would result in code like:会产生如下代码:

model: model:

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

startup.cs:启动.cs:

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

class with DI: class 带 DI:

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

Question问题

If you start a new web application and choose for example a web api, the standard appsettings will contain the appsetting “Microsoft.Hosting.Lifetime”.如果您启动一个新的 web 应用程序并选择例如 web api,则标准应用设置将包含应用设置“Microsoft.Hosting.Lifetime”。 Like in the picture in the beginning.就像一开始的图片一样。 So how should I implement that setting in my model?那么我应该如何在我的 model 中实现该设置?

I Googled a lot, searched on stackoverflow and even my favorite blog writer Rick Strahl ( https://weblog.west-wind.com/posts/2017/dec/12/easy-configuration-binding-in-aspnet-core-revisited ) skipped the part with the logging section.我在 Google 上搜索了很多,在 stackoverflow 甚至我最喜欢的博客作者 Rick Strahl 上进行了搜索( https://weblog.west-wind.com/posts/2017/dec/12/easy-configuration-binding-in-aspnet-core-revisited ) 跳过了记录部分的部分。 I hope someone can help me with this one.我希望有人可以帮助我解决这个问题。 Thanks!谢谢!

Why do I want that property?: because reasons;)为什么我想要那个属性?:因为原因;)

Thanks to Asawyer, we came to a solution.感谢 Asawyer,我们找到了解决方案。 Maybe there is a better one, but for now we fixed it as follows:也许有更好的,但现在我们将其修复如下:

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

startup.cs:启动.cs:

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

class with DI: class 带 DI:

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

In .NET 6, the ConfigurationKeyNameAttribute has been added to support this在 .NET 6 中,添加了ConfigurationKeyNameAttribute来支持这个

In a similar situation, I used a less strongly typed solution, as the idea to read the specific settings in Startup.cs did not feel like a clean solution.在类似的情况下,我使用了一个不那么强类型的解决方案,因为阅读 Startup.cs 中的特定设置的想法并不像一个干净的解决方案。

Here's my appsettings.json:这是我的 appsettings.json:

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

And to read the "Logging.LogLevel" section I used a Dictionary instead为了阅读“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; }
}

and to use this dictionary<string, string> while pretending that it's keys are not free style strings并在假装它的键不是自由样式字符串的同时使用这个字典<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.

相关问题 从类库项目访问appsettings.json文件设置 - Access appsettings.json file setting from the class library project Serilog 自定义接收器 - 使用 appsettings.json 设置最低级别不起作用 - Serilog custom sink - setting minimum level with appsettings.json not working 如何从programs.cs Blazor应用程序中的配置中获取appsettings.json中设置的值 - How do I get the value of a setting in appsettings.json from configuration in programs.cs Blazor app 如何从 appsettings.json 读取列表并将其绑定到 mvc dropdownlistfor() - How to read a list from appsettings.json and bind it in the mvc dropdownlistfor() 绑定来自 appsettings.json 的字符串列表 - Bind list of string from appsettings.json 使用 ENV 变量在 appsettings.json 中设置键 - ASP.NET Core 3.1 Docker - Setting keys in appsettings.json with ENV variables - ASP.NET Core 3.1 Docker 如何为不同的操作系统覆盖 appsettings.json? - How to override appsettings.json for different OS? 如何在 appsettings.json 中加载多态对象 - How to load polymorphic objects in appsettings.json 如何将我的 appsettings.json 文件中的 Microsoft.Hosting.Lifetime 绑定到我的 C# 应用程序中的 AppSettings class? - How to I bind Microsoft.Hosting.Lifetime in my appsettings.json file to my AppSettings class in my C# application? 如何使用值数组读取appsettings.json - How to read appsettings.json with array of values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM