简体   繁体   English

无法从 appsettings 获取对象列表

[英]Can't get list of object from appsettings

I have app settings that look like this:我的应用程序设置如下所示:

"Tenants": [
  {
    "Id": "00000000-0000-0000-0000-00000000000",
    "ConnectionString": "dbstring"
  },
  {
    "Id": "00000000-0000-0000-0000-00000000001",
    "ConnectionString": "dbstring"
  }
]

and an object that looks like this:和一个看起来像这样的对象:

public class TenantSecrets
{
    public string ConnectionString { get; set; }
    public Guid Id { get; set; }
}

public class Tenants : List<TenantSecrets> { }

When I try to either configure them or bind them like so:当我尝试配置它们或像这样绑定它们时:

services.Configure<Tenants>(Configuration.GetSection("Tenants"));

var tenants = new Tenants();
Configuration.Bind("Tenants", tenants);

The list is always empty, does anyone know why or how I can debug it?该列表始​​终为空,有谁知道为什么或如何调试它? I can see the list in the appsettings configuration reader when I debug it, but the object never seems to map.调试时,我可以在 appsettings 配置阅读器中看到该列表,但该对象似乎从未映射过。

Edit: Good, you spotted this mistake, it happens it works perfectly.编辑:很好,你发现了这个错误,它恰好工作得很好。 I don't want to write a useless answer so here is the version using the Options pattern .我不想写一个无用的答案,所以这里是使用选项模式的版本。

Configure the Tenants class in the Startup.cs file (method ConfigureServices )Startup.cs文件中配置Tenants类(方法ConfigureServices

services.Configure<Tenants>(Configuration.GetSection("Tenants"));

Then, inject the IOptions<Tenants> service in a controller constructor.然后,在控制器构造函数中注入IOptions<Tenants>服务。

private readonly Tenants _tenants;

public HomeController(IOptions<Tenants> tenantOptions)
{
    _tenants = tenantOptions.Value;
}

好吧,我想通了,结果我用于 Id 的空 guid 比 guid 少一个 0,我现在知道配置到 poco 映射失败了,所以对于其他有问题的人,请检查您的数据符合数据类型

C# 强类型ConfigurationBuilder列表<object>从应用设置<div id="text_translate"><p>真的在为这个苦苦挣扎——它应该这么难吗?!</p><p> 我的 appsettings 中有一个简单的 object 数组:</p><pre> "AppSettings": { "Names": [ { "Id": "1", "Name": "Mike" }, { "Id": "2", "Name": "John" } ] }</pre><p> 然后我有一个 class</p><pre> public class AppSettings { public List&lt;Names&gt; Names { get; set; } = new List&lt;Names&gt;(); } public class Names { public string Id { get; set; } public string Name { get; set; } }</pre><p> 我在我的应用设置中读到:</p><pre> var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build(); var config = new AppSettings();</pre><p> 这就是出错的地方:</p><pre> config.Names = configuration.GetSection("AppSettings:Names") //&lt;&lt;&lt;&lt; what do I do here?</pre><p> 似乎它都与IConfigurationSection相关,这没有帮助。</p></div></object> - C# strongly typed ConfigurationBuilder List<object> from appsettings

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从appSettings中读取 - Can't read from appSettings 无法获取对象属性(的列表)以从局部视图进行绑定 - Can't get (list of) object properties to bind from partial view C# 强类型ConfigurationBuilder列表<object>从应用设置<div id="text_translate"><p>真的在为这个苦苦挣扎——它应该这么难吗?!</p><p> 我的 appsettings 中有一个简单的 object 数组:</p><pre> "AppSettings": { "Names": [ { "Id": "1", "Name": "Mike" }, { "Id": "2", "Name": "John" } ] }</pre><p> 然后我有一个 class</p><pre> public class AppSettings { public List&lt;Names&gt; Names { get; set; } = new List&lt;Names&gt;(); } public class Names { public string Id { get; set; } public string Name { get; set; } }</pre><p> 我在我的应用设置中读到:</p><pre> var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build(); var config = new AppSettings();</pre><p> 这就是出错的地方:</p><pre> config.Names = configuration.GetSection("AppSettings:Names") //&lt;&lt;&lt;&lt; what do I do here?</pre><p> 似乎它都与IConfigurationSection相关,这没有帮助。</p></div></object> - C# strongly typed ConfigurationBuilder List<object> from appsettings 无法从 appsettings.json 读取数据 - Can't read data from appsettings.json 无法从Web.Config中读取appSettings值 - Can't read appSettings value from Web.Config 无法来自 appsettings.json 的 IConfiguration 提供程序的 map 实例 - Can't map instance from IConfiguration provider of appsettings.json 无法从外部 appSettings 文件中读取密钥? - can't read Keys from external appSettings file? 获取列表类型 <T> 来自对象 - Get type of List<T> from object 从List获取特定类型的对象<T> - Get specific type of object from List<T> 从代码中获取appSettings configSource - Get appSettings configSource from code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM