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.
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();
}
I've found a solution from Tsengs answer but wish to describe it here for clarity. The solution is found in the answer to another question however the question is quite different (and I've also expanded upon the answer) so I do not believe this question should be marked as a duplicate.
The answer is here
The solution is to setup different environment variable values on each IIS site for the key ASPNETCORE_ENVIRONMENT
The steps to do so are:
Configuration Editor
.Configuration Editor
system.webServer/aspNetCore
(RC2 and RTM) or system.webServer/httpPlatform
(RC1) in Section
comboboxApplicationhost.config ...
in From
combobox.enviromentVariables
element and open edit window.Alternatively, you can modify your applicationHost.config
file (normally located at C:\Windows\System32\inetsrv\config\applicationHost.config
And add the following entry under the root <Configuration>
tag, where "my-iis-site" is the name of your IIS site.
<location path="my-iis-site">
<system.webServer>
<aspNetCore>
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="DEV" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
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 .
The dialog you linked in the picture is only to configure "launchSettings.json". This file is not used by your application .
It is only used by Visual Studio to set the environment and open an URL in the browser when you hit F5 and nothing else.
When you want to switch environments, you need to setup an environment variable before launching. How to do this, depends on your environment.
setx ASPNETCORE_ENVIRONMENT "Development"
$Env:ASPNETCORE_ENVIRONMENT = "Development"
export ASPNETCORE_ENVIRONMENT="Development"
ASPNETCORE_ENVIRONMENT="Development" dotnet run
Yes it is machine specific (except for Linux, which you can do per command). However, in IIS you can do that too either via different app pools or by following this answers instructions to add it to IIS
如果您使用的是 azure,请将具有相应值的 ASPNETCORE_ENVIRONMENT 添加到 webapp 一侧的应用程序设置中,然后它将获取正确的 appsettings.{value}.json 文件
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.