7
votes

I have an appsettings.json and appsettings.Development.json. I need to assign the SmtpServer name depending on the environment.

The config file in appsettings.json is:

{ 
  "EmailConfiguration": {
   "SmtpServer": "mail.MYDOMAIN.com"
  }
}

And in appsettings.Development.json is:

{ 
  "EmailConfiguration": {
    "SmtpServer": "mail.MYLOCAL.com"
  }
}

When I assign the configuration in Startup ConfigureServices() like such:

var emailconfig = Configuration.GetSection("EmailConfiguration").Get<EmailConfiguration>();
services.AddSingleton<IEmailConfiguration>(emailconfig);

It always uses appsettings.json ('mail.MYDOMAIN.com') and NOT appsettings.Development.json.

How do I modify this code to use the correct environment settings?

1
What build configuration are you running under?Dan Schnau
In VS I'm using the Debug configuration and within that I have: Environment variables Name: ASPNETCORE_ENVIRONMENT Value: Development.P-Finny
you sould read the doc regarding configuration I think : docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/…agua from mars
Looks like P-Finny did everything under that doc page. @P-Finny Have you tried killing iisexpress?Dan Schnau
@DanSchnau I did one better and restarted my machine to make sure IISExpress was cleared out. I've put in a variable in the Startup method and confirmed my environment is 'Development'. After reading the documentation again on setup I am not seeing anything that specifically says returning a Configuration.GetSection() will get the environment specific variable. Do you know if that should happen? Or do I need to call a more environment specific method?P-Finny

1 Answers

5
votes

The answer is that all I needed was the environment variables in the 2 different appsettings files. My problem was a missing closing bracket in one of my configuration settings which meant that the appsettings.Development.json would not replace appsettings.json variables since they didn't match. Oops.....