在ASP.Net Core App的调试期间未注入appSetting.Development.json

时间:2019-02-22 15:45:29

标签: c# asp.net-core asp.net-core-2.1

我的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

有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

我在为此寻找麻烦和官方资源时遇到了麻烦,但是本质上问题是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"
    ]
  }
}

然后,您将只在开发中使用一个地址,而在生产中使用两个地址。