Options Values are always empty/null for a Dictionary configuration section

huangapple go评论91阅读模式
英文:

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:

&quot;Tenants&quot;: {
    &quot;First&quot;: {
        &quot;Name&quot;: &quot;Example 1&quot;
    },
    &quot;Second&quot;: {
        &quot;Name&quot;: &quot;Example 2&quot;
    }
}

Here are my Options models:

public class Tenant
{
    public string Name { get; set; }
}

public class TenantsOptions
{
    public Dictionary&lt;string, Tenant&gt; Tenants { get; set; } = new Dictionary&lt;string, Tenant&gt;();
}

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // ...
        services.Configure&lt;TenantsOptions&gt;(Configuration.GetSection(&quot;Tenants&quot;));

        // Other service configurations...
    }
}

and when I try to use it somewhere:

public class TenantController : ControllerBase
{
    private readonly TenantsOptions _tenantsOptions;

    public TenantController(IOptions&lt;TenantsOptions&gt; tenantsOptions)
    {
        _tenantsOptions = tenantsOptions.Value;
    }

    [HttpGet(&quot;tenants&quot;)]
    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(&quot;Tenants&quot;).Get&lt;Dictionary&lt;string, Tenant&gt;&gt;();

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&lt;TenantsOptions&gt;(Configuration/*.GetSection(&quot;Tenants&quot;)*/);

Option 2

Change your JSON code to the following

&quot;Tenants&quot;: {
  &quot;Tenants&quot;: {// &lt;--
    &quot;First&quot;: {
      &quot;Name&quot;: &quot;Example 1&quot;
    },
    &quot;Second&quot;: {
      &quot;Name&quot;: &quot;Example 2&quot;
    }
  }// &lt;--
},

huangapple
  • 本文由 发表于 2023年7月28日 06:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定