appSetting.Development.json 在 ASP.Net 核心应用的调试期间未注入

appSetting.Development.json not injected during Debug on ASP.Net Core App

我的 appSetting.json;

中有以下内容
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "MailServiceSettings": {
    "SmtpServer": "<server>",
    "ToAddresses": [
      "email@domain.com",
      "email2@domain.com"
    ],
    "UserName": "username",
    "Password": "password"
  }
}

并且在 appSettings.Development.json 中我有一个微妙的变化;

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "MailServiceSettings": {
    "SmtpServer": "<server>",
    "ToAddresses": [
      "development@domain.com"
    ],
    "UserName": "username",
    "Password": "password"
  }
}

这样我就可以在我的本地主机中发送邮件发件人设置,而不会轰炸实时邮箱。

但是,当我 运行 在调试时 appSettings.json 的设置被注入 appSettings.Development.json.

我的 Program.cs 使用默认 WebHostBuilder;

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args)
            .Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
}

并在我的 StartUp.cs;

中按照以下方式设置 DI
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.Configure<MailServiceSettings>(Configuration.GetSection("MailServiceSettings"));

        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }

然后,当我调试并中断配置时,我可以看到 appSettings.Development.json 已被读取(因为我可以在调试时深入到 Configuration 部分,我可以看到它们被添加为一个额外的条目,我相信 WebHost.CreateDefaultbuilder 默认添加 env.EnvironmentName 个文件)。

但是,当我随后实例化一个控制器方法时;

public ContactController(IOptions<MailServiceSettings> mailSettings, IHostingEnvironment hostingEnvironment)
{
    _mailSettings = mailSettings;
    _hostingEnvironment = hostingEnvironment;
}

我发现来自 appSettings.json 的 2x 电子邮件地址被注入而不是 appSettings.Development.json

我也在 运行 时检查了 env.IsDevelopment(),这正在返回 true

谁能告诉我我做错了什么?

我很难找到官方来源,但本质上问题是 IConfiguration 基本上是一个字典,来自配置源的键和值被扁平化到其中。换句话说,在一天结束时,您实际得到的伪代码如下所示:

["MailServiceSettings:ToAddresses[0]"] = "email@domain.com"
["MailServiceSettings:ToAddresses[1]"] = "email2@domain.com"

然后,当您的 appsettings.Development.json 配置出现时:

["MailServiceSettings:ToAddresses[0]"] = "development@domain.com"

换句话说,您的配置中还有两项。解决此问题的唯一方法是仅在特定于环境的配置中进行此类设置。如果您将其从 appsettings.json 中完全删除,然后执行:

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "MailServiceSettings": {
    "SmtpServer": "<server>",
    "UserName": "username",
    "Password": "password"
  }
}

appsettings.Development.json

{
  "MailServiceSettings": {
    "ToAddresses": [
      "development@domain.com"
    ]
  }
}

appsettings.Production.json

{
  "MailServiceSettings": {
    "ToAddresses": [
      "email@domain.com",
      "email2@domain.com"
    ]
  }
}

然后,您将正确地只有一个开发地址和两个生产地址。