[英]How can I load an empty object into a dictionary from appsettings.json?
I'm trying to load values into a custom options class on app startup in C#/.net6.我正在尝试在 C#/.net6 中的应用程序启动时将值加载到自定义选项 class 中。 Some of the input from
appsettings.json
is not being loaded as I would like.来自
appsettings.json
的一些输入没有按照我的意愿加载。
My options class looks like this:我的选项 class 如下所示:
public class CustomOptions
{
public Dictionary<string, CustomInfo> MyOptions { get; set; }
}
public class CustomInfo
{
public Dictionary<string, string> DictionaryField { get; set; } = new Dictionary<string, string>();
public List<string> ListField { get; set; } = new List<string>();
}
and appsettings.json
contains the following: appsettings.json
包含以下内容:
"CustomOptions" : {
"MyOptions": {
"OptionOne": {},
"OptionTwo": {
"DictionaryField": {
"ValueOne": "somevalue",
"ValueTwo": "someothervalue"
},
"ListField": [
"ListItem"
]
}
}
}
and loading the options in Program.cs with并加载 Program.cs 中的选项
builder.Services.AddOptions<EndpointOptions>().Bind(builder.Configuration.GetSection(EndpointOptions.Key));
As it stands, OptionTwo is loaded correctly with all fields set, yet OptionOne does not even appear in the injected IOptions<TOptions>
service.就目前而言,OptionTwo 已正确加载所有字段集,但 OptionOne 甚至没有出现在注入的
IOptions<TOptions>
服务中。
What I would like is for OptionOne to be loaded into the MyOptions
dict with a default CustomInfo
object or null value.我想要的是将 OptionOne 加载到具有默认
CustomInfo
object 或 null 值的MyOptions
字典中。 Is this possible?这可能吗?
Edit: I need to be able to add more options beyond OptionOne and OptionTwo at some point, and ideally would be able to do this through appsettings.json
, hence the need for a dictionary in MyOptions
class. All these options will still follow the CustomInfo class.编辑:我需要能够在某些时候添加 OptionOne 和 OptionTwo 之外的更多选项,并且理想情况下能够通过
appsettings.json
执行此操作,因此需要在MyOptions
class 中添加字典。所有这些选项仍将遵循 CustomInfo class。
The JsonConfigurationFileParser
ignores any empty object and simply add value of null
as configuration value, that is ignored during binding . JsonConfigurationFileParser
忽略任何空的 object 并简单地将null
的值添加为配置值,该值在绑定期间被忽略。
If you really need both options to be added to the dictionary, you could add at least one property to OptionOne
section:如果您确实需要将这两个选项都添加到字典中,您可以将至少一个属性添加到
OptionOne
部分:
"OptionOne": {
"DictionaryField": {}
}
Frankly, in the sense of low memory allocating, it makes sense to not create an empty instance of CustomInfo
, but use the TryGetValue
method on dictionary to test if the value exists.坦率地说,在低 memory 分配的意义上,不创建
CustomInfo
的空实例是有意义的,而是使用字典上的TryGetValue
方法来测试该值是否存在。
the easiest way is to use this code最简单的方法是使用此代码
CustomOptions customOptions = configuration.GetSection("CustomOptions").Get<CustomOptions>();
var myOptions = customOptions.MyOptions;
classes班级
public class CustomOptions
{
public MyOptions MyOptions { get; set; }
}
public class MyOptions
{
public CustomInfo OptionOne { get; set; }
public OptionTwo OptionTwo { get; set; }
}
public class CustomInfo
{
public Dictionary<string, string> DictionaryField { get; set; } = new Dictionary<string, string>();
public List<string> ListField { get; set; } = new List<string>();
}
public class DictionaryField
{
public string ValueOne { get; set; }
public string ValueTwo { get; set; }
}
public class OptionTwo
{
public DictionaryField DictionaryField { get; set; }
public List<string> ListField { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.