小编Wor*_*kle的帖子

.Net 6 config.GetSection() 未按预期运行

我试图在 .net 6 项目中获取配置部分,我遇到的问题是它GetSection()没有按预期运行,它不返回该部分,它只是空的。这是一些代码片段:

public static IServiceCollection AddServices(this IServiceCollection services, IConfiguration config)
{
    var e = config["EmailConfig"];
    var s = config.GetSection("EmailConfig:Server");
    IConfigurationSection emailConfigSection = config.GetSection(EmailConfig.Name);
    services.Configure<EmailConfig>(emailConfigSection);

    return services;
}
Run Code Online (Sandbox Code Playgroud)

EmailConfig.Name是一个常数并且等于"EmailConfig"

变量值看起来如下所示e = nulls = "server"并且emailConfigSection = null,那么原因是什么?为什么我无法获得完整部分

appsettings.json如果需要的话:

{
 "EmailConfig": {
    "Server": "server",
    "Port": 587,
    "UseSSL": true,
    "Username": "name",
    "Password": "password",
    "DefaultSender": "sender"
  }
}
Run Code Online (Sandbox Code Playgroud)

调试时配置

.net c# .net-6.0

3
推荐指数
1
解决办法
8155
查看次数

如何展平列表列表?

我有一个查询,它与 LegalEntity 表具有一对多关系,我的目标是获取所有 LegalEntityId 的列表。此时,我有以下 select 生成List<IEnumerable<string>>,是否有一种快速有效的方法,无需循环和创建单独的列表即可将内部列表扁平化为一个。

var legalEntityIds = query.Select(x => x.LegalEntities.Select(y => y.LegalEntityId)).ToList();
Run Code Online (Sandbox Code Playgroud)

.net c#

1
推荐指数
1
解决办法
2059
查看次数

标签 统计

.net ×2

c# ×2

.net-6.0 ×1