I have the following in my appSetting.json;
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"MailServiceSettings": {
"SmtpServer": "<server>",
"ToAddresses": [
"[email protected]",
"[email protected]"
],
"UserName": "username",
"Password": "password"
}
}
and in appSettings.Development.json i have a subtle change;
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"MailServiceSettings": {
"SmtpServer": "<server>",
"ToAddresses": [
"[email protected]"
],
"UserName": "username",
"Password": "password"
}
}
This is so I can text the mail sender settings in my localhost without bombarding the live mailbox.
However, when I run in debug the settings from appSettings.json are being injected insted of the appSettings.Development.json.
My Program.cs is using the default WebHostBuilder;
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
}
and setup the DI as following in my 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";
});
}
Then when I debug and break on the configuration I can see that the appSettings.Development.json have been read (as I can drill down into the Configuration sections when debugging I can see they are added as an additional entry, and I believe WebHost.CreateDefaultbuilder adds the env.EnvironmentName files by default).
However, when I then instantiate an controller method;
public ContactController(IOptions<MailServiceSettings> mailSettings, IHostingEnvironment hostingEnvironment)
{
_mailSettings = mailSettings;
_hostingEnvironment = hostingEnvironment;
}
I am finding that the 2x email address from the appSettings.json are injected instead of the appSettings.Development.json
I have also checked env.IsDevelopment() at runtime and this is returning true.
Can anyone tell me what I am doing wrong here?
Copy to Output Directoryset toCopy AlwaysinAdvancedsection?appSettings.jsonis overriding the development version, for example if I remove the 2x email address completely fromappSettings.jsonthe 1x email address is injected from theappSettings.Developement.jsonis this correct?ASPNETCORE_ENVIRONMENTvalue inProperties/defaultSettings.json? Or in your host environment variables?