英文:
Options Values are always empty/null for a Dictionary configuration section
问题
我正在尝试将这个appsettings
部分绑定到我的Options,但它的工作方式不如预期。
以下是我在appsettings.json
中的配置部分:
"Tenants": {
"First": {
"Name": "Example 1"
},
"Second": {
"Name": "Example 2"
}
}
这是我的Options模型:
public class Tenant
{
public string Name { get; set; }
}
public class TenantsOptions
{
public Dictionary<string, Tenant> Tenants { get; set; } = new Dictionary<string, Tenant>();
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// ...
services.Configure<TenantsOptions>(Configuration.GetSection("Tenants"));
// 其他服务配置...
}
}
当我尝试在某处使用它时:
public class TenantController : ControllerBase
{
private readonly TenantsOptions _tenantsOptions;
public TenantController(IOptions<TenantsOptions> tenantsOptions)
{
_tenantsOptions = tenantsOptions.Value;
}
[HttpGet("tenants")]
public IActionResult GetTenants()
{
var tenants = _tenantsOptions.Tenants.Values; // 这总是为空/Null,为什么?
return Ok(tenants);
}
}
例如,这样做可以正常工作并返回值:
var options = builder.Configuration.GetSection("Tenants").Get<Dictionary<string, Tenant>>();
现在我看到了许多实现相同功能的方法(例如使用从配置中获取值的.GetValue
),但我想以这种方式实现,只从DI中获取Options,这是否可能,或者我漏掉了什么?
英文:
I am trying to bind this appsettings section to my Options but it is not working as expected
here is my config section inside appsettings.json
:
"Tenants": {
"First": {
"Name": "Example 1"
},
"Second": {
"Name": "Example 2"
}
}
Here are my Options models:
public class Tenant
{
public string Name { get; set; }
}
public class TenantsOptions
{
public Dictionary<string, Tenant> Tenants { get; set; } = new Dictionary<string, Tenant>();
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// ...
services.Configure<TenantsOptions>(Configuration.GetSection("Tenants"));
// Other service configurations...
}
}
and when I try to use it somewhere:
public class TenantController : ControllerBase
{
private readonly TenantsOptions _tenantsOptions;
public TenantController(IOptions<TenantsOptions> tenantsOptions)
{
_tenantsOptions = tenantsOptions.Value;
}
[HttpGet("tenants")]
public IActionResult GetTenants()
{
var tenants = _tenantsOptions.Tenants.Values; // this is always empty/null why?
return Ok(tenants);
}
}
For example this works and it is giving me the values:
var options = builder.Configuration.GetSection("Tenants").Get<Dictionary<string, Tenant>>();
Now I've seen many ways to achieve the same thing (using .GetValue
from config for example) but I want to do it this way only getting the options from the DI, is this possible or I am missing something?
答案1
得分: 0
代码似乎期望在另一个“Tenants”对象内部存在一个“Tenants”对象,但缺少了。以下是解决此问题的两种方法。
选项 1
删除 GetSection 方法调用。
services.Configure<TenantsOptions>(Configuration/*.GetSection("Tenants")*/);
选项 2
将您的 JSON 代码更改为以下内容:
"Tenants": {
"Tenants": {// <--
"First": {
"Name": "Example 1"
},
"Second": {
"Name": "Example 2"
}
}// <--
},
英文:
The code seems to expect a Tenants
object inside an another Tenants
object, which is missing. Here are a couple ways of solving this.
Option 1
Remove the GetSection method call.
services.Configure<TenantsOptions>(Configuration/*.GetSection("Tenants")*/);
Option 2
Change your JSON code to the following
"Tenants": {
"Tenants": {// <--
"First": {
"Name": "Example 1"
},
"Second": {
"Name": "Example 2"
}
}// <--
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论