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.
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.
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.
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.
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(); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With