Mat*_*ynn 9 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"
}
}
Run Code Online (Sandbox Code Playgroud)
而appSettings.Development.json我也有了微妙的变化;
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"MailServiceSettings": {
"SmtpServer": "<server>",
"ToAddresses": [
"development@domain.com"
],
"UserName": "username",
"Password": "password"
}
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以在本地主机中发送邮件发件人设置文本,而无需轰炸实时邮箱。
但是,当我在调试中运行时,设置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)
}
Run Code Online (Sandbox Code Playgroud)
并在 my 中按以下方式设置 DI StartUp.cs;
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";
});
}
Run Code Online (Sandbox Code Playgroud)
然后,当我调试并中断配置时,我可以看到appSettings.Development.json已读取这些文件(因为我可以在调试时深入了解这些Configuration部分,我可以看到它们被添加为附加条目,并且我相信默认情况下WebHost.CreateDefaultbuilder会添加文件)。env.EnvironmentName
然而,当我实例化一个控制器方法时;
public ContactController(IOptions<MailServiceSettings> mailSettings, IHostingEnvironment hostingEnvironment)
{
_mailSettings = mailSettings;
_hostingEnvironment = hostingEnvironment;
}
Run Code Online (Sandbox Code Playgroud)
我发现注入的是 2x 电子邮件地址,appSettings.json而不是appSettings.Development.json
我还在env.IsDevelopment()运行时检查过,这正在返回true。
谁能告诉我我在这里做错了什么?
我在追踪官方来源时遇到了麻烦,但本质上问题是它基本上IConfiguration是一个字典,并且来自配置源的键和值被扁平化到其中。换句话说,最终,您实际得到的内容类似于以下伪代码:
["MailServiceSettings:ToAddresses[0]"] = "email@domain.com"
["MailServiceSettings:ToAddresses[1]"] = "email2@domain.com"
Run Code Online (Sandbox Code Playgroud)
然后,当你的appsettings.Development.json配置进来时:
["MailServiceSettings:ToAddresses[0]"] = "development@domain.com"
Run Code Online (Sandbox Code Playgroud)
换句话说,配置中仍然有两项。解决这个问题的唯一方法是仅在特定于环境的配置中进行此类设置。如果您完全删除它appsettings.json,然后执行以下操作:
应用程序设置.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"MailServiceSettings": {
"SmtpServer": "<server>",
"UserName": "username",
"Password": "password"
}
}
Run Code Online (Sandbox Code Playgroud)
appsettings.Development.json
{
"MailServiceSettings": {
"ToAddresses": [
"development@domain.com"
]
}
}
Run Code Online (Sandbox Code Playgroud)
appsettings.Production.json
{
"MailServiceSettings": {
"ToAddresses": [
"email@domain.com",
"email2@domain.com"
]
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您将正确地仅在开发中拥有一个地址,在生产中拥有两个地址。
| 归档时间: |
|
| 查看次数: |
9916 次 |
| 最近记录: |