[英]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.