8

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?

6
  • What build configuration are you running under? Commented Jan 2, 2018 at 19:07
  • In VS I'm using the Debug configuration and within that I have: Environment variables Name: ASPNETCORE_ENVIRONMENT Value: Development. Commented Jan 2, 2018 at 19:10
  • you sould read the doc regarding configuration I think : learn.microsoft.com/en-us/aspnet/core/fundamentals/… Commented Jan 2, 2018 at 19:27
  • Looks like P-Finny did everything under that doc page. @P-Finny Have you tried killing iisexpress? Commented Jan 2, 2018 at 19:30
  • 1
    @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? Commented Jan 2, 2018 at 19:46

1 Answer 1

7

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.....

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.