Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I transform appsettings.json in a .NET Core MVC project?

Tags:

I've added additional json config files to my project

appsettings.DEV.json appsettings.QA.json 

and loaded them in the Startup function based on the environment:

public Startup(IHostingEnvironment env) {     var builder = new ConfigurationBuilder()         .SetBasePath(env.ContentRootPath)         .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)         .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);     ... 

And I understand how to change the environment: modify the value of the ASPNETCORE_ENVIRONMENT environment variable in project properties. However, there does not appear to be the ability to specify different environment variables depending on the configuration, the dropdownlist is labeled "N/A" and disabled.

The only option I see is to manually change the environment variable value, to change which appsettings are used. I'm sure there is a way to do it automatically, or else how would you ever use CI? (other than using a script to change the environment variable, there has to be an easier way).

The goal here is to setup automated builds and continuous integration for three environments: DEV, QA, and PROD. DEV and QA are on the same machine, so setting the environment variable that specifies the environment manually is not an option.

Project Properties : Debug section

like image 372
James Wierzba Avatar asked Mar 28 '17 15:03

James Wierzba


People also ask

How do I add Appsettings json to project?

Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.

What is Appsettings json file in ASP.NET Core MVC?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.

How can I get Connection String from Appsettings json in NET Core?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new Connection String entry is added.


2 Answers

Determine EnvironmentName from Build Type

For anybody that would like to set the EnvironmentName based on the build type, there is the handy .UseEnvironment(environmentName) on WebHostBuilder (found in Program Main).

As long as the appropriate compilation symbols are set against the build configurations in your project, you can do something like this to determine the EnvironmentName:

public static void Main(string[] args) {     string environmentName; #if DEBUG     environmentName = "Development"; #elif STAGING     environmentName = "Staging"; #elif RELEASE     environmentName = "Production"; #endif      var host = new WebHostBuilder()         .UseKestrel()         .UseEnvironment(environmentName)         .UseContentRoot(Directory.GetCurrentDirectory())         .UseIISIntegration()         .UseStartup<Startup>()         .UseApplicationInsights()         .Build();      host.Run(); } 
like image 92
Carl Sharman Avatar answered Oct 18 '22 16:10

Carl Sharman


If you are using the default code in Program.cs, you don't need to do anything beyond creating the two files in the project.

The default code in Program.cs looks like this:

public static IWebHost BuildWebHost(string[] args) =>             WebHost.CreateDefaultBuilder(args)                 .UseStartup<Startup>()                 .Build(); 

Here's what that's actually doing:

public static IWebHostBuilder CreateDefaultBuilder(string[] args) {     var builder = new WebHostBuilder();      ...      builder.ConfigureAppConfiguration((hostingContext, config) =>     {         var env = hostingContext.HostingEnvironment;          config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)               .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);     });      ...      return builder; } 

env.EnvironmentName is set to the value of the ASPNETCORE_ENVIRONMENT environment variable, so all you have to do is create the appsettings.{ASPNETCORE_ENVIRONMENT}.json file and it will automatically get merged.

Additional note: to get the two files to actually merge, use this syntax:

var appSettings = Configuration.GetSection("AppSettings").Get<AppSettings>(); 

not:

var appSettings = new AppSettings(); Configuration.Bind("AppSettings", appSettings); return appSettings; 

The latter will not returned the merged data.

Thanks to Shawn Wildermuth for this.

like image 35
Greg Gum Avatar answered Oct 18 '22 17:10

Greg Gum