我正在尝试在我的应用程序中检索配置。
我已将 IConfiguration 传递给需要提取一些设置的服务类。
这个类看起来有点像这样:
private IConfiguration _configuration;
public Config(IConfiguration configuration)
{
_configuration = configuration;
}
public POCO GetSettingsConfiguration()
{
var section = _configuration.GetSection("settings") as POCO;
return section;
}
在调试中,我可以看到 _configuration 确实包含设置,但我的“部分”只是简单地返回为 null。
我知道我可以尝试设置要在启动时创建的 Poco,然后作为依赖项注入,但由于设置,如果可能的话,我宁愿在注入的 IConfiguration 的单独类中进行。
我的 appsettings.json 有这些值:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"settings": {
"Username": "user",
"Password": "password",
"Domain": "example.com",
"URL": "http://service.example.com"
}
}
我的 poco 类如下所示:
public class POCO
{
public string URL { get; set; }
public string Username { get; set; }
public SecureString Password { get; set; }
public string Domain { get; set; }
}