有什么方法可以以强类型的方式从appsettings.json中读取多态对象?贝娄是我所需要的非常简化的例子。
我有多个应用程序组件,在这里名为Features
。这些组件由工厂在运行时创建。我的设计意图是,每个组件均由单独的强类型选项进行配置。在此示例中,FileSizeCheckerOptions
和PersonCheckerOption
是这些实例。每个功能可以使用不同的选项多次包含。
但是使用现有的Asp.Net Core配置系统,我无法阅读 polymorphic 强类型选项。如果Json反序列化程序将读取设置,则可以使用something like this。但这不是appsettings.json的情况,在这里,选项只是键值对。
appsettings.json
{
"DynamicConfig":
{
"Features": [
{
"Type": "FileSizeChecker",
"Options": { "MaxFileSize": 1000 }
},
{
"Type": "PersonChecker",
"Options": {
"MinAge": 10,
"MaxAge": 99
}
},
{
"Type": "PersonChecker",
"Options": {
"MinAge": 15,
"MaxAge": 20
}
}
]
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.Configure<FeaturesOptions>(Configuration.GetSection("DynamicConfig"));
ServiceProvider serviceProvider = services.BuildServiceProvider();
// try to load settings in strongly typed way
var options = serviceProvider.GetRequiredService<IOptions<FeaturesOptions>>().Value;
}
其他定义
public enum FeatureType
{
FileSizeChecker,
PersonChecker
}
public class FeaturesOptions
{
public FeatureConfig[] Features { get; set; }
}
public class FeatureConfig
{
public FeatureType Type { get; set; }
// cannot read polymorphic object
// public object Options { get; set; }
}
public class FileSizeCheckerOptions
{
public int MaxFileSize { get; set; }
}
public class PersonCheckerOption
{
public int MinAge { get; set; }
public int MaxAge { get; set; }
}
答案 0 :(得分:1)
回答这个问题的关键是要知道密钥是如何生成的。在您的情况下,键/值对将是:
DynamicConfig:Features:0:Type
DynamicConfig:Features:0:Options:MaxFileSize
DynamicConfig:Features:1:Type
DynamicConfig:Features:1:Options:MinAge
DynamicConfig:Features:1:Options:MaxAge
DynamicConfig:Features:2:Type
DynamicConfig:Features:2:Options:MinAge
DynamicConfig:Features:2:Options:MaxAge
注意数组的每个元素是如何用 DynamicConfig:Features:{i}
表示的。
要知道的第二件事是,您可以使用 ConfigurationBinder.Bind
方法将配置的任何部分映射到对象实例:
var conf = new PersonCheckerOption();
Configuration.GetSection($"DynamicConfig:Features:1:Options").Bind(conf);
当我们将所有这些放在一起时,我们可以将您的配置映射到您的数据结构:
services.Configure<FeaturesOptions>(opts =>
{
var features = new List<FeatureConfig>();
for (var i = 0; ; i++)
{
// read the section of the nth item of the array
var root = $"DynamicConfig:Features:{i}";
// null value = the item doesn't exist in the array => exit loop
var typeName = Configuration.GetValue<string>($"{root}:Type");
if (typeName == null)
break;
// instantiate the appropriate FeatureConfig
FeatureConfig conf = typeName switch
{
"FileSizeChecker" => new FileSizeCheckerOptions(),
"PersonChecker" => new PersonCheckerOption(),
_ => throw new InvalidOperationException($"Unknown feature type {typeName}"),
};
// bind the config to the instance
Configuration.GetSection($"{root}:Options").Bind(conf);
features.Add(conf);
}
opts.Features = features.ToArray();
});
注意:所有选项都必须派生自 FeatureConfig
才能工作(例如 public class FileSizeCheckerOptions : FeatureConfig
)。您甚至可以使用反射来自动检测从 FeatureConfig
继承的所有选项,以避免切换类型名称。
注意 2:如果您愿意,您还可以将配置映射到 Dictionary
或 dynamic
对象;查看我对Bind netcore IConfigurationSection to a dynamic object的回答。