I have ASP.NET core web application. I have configured the web application on our web server and set the ASPNETCORE_ENVIRONMENT
variable to Development
. I set this variable at machine level like shown in the picture below.
Now on the same machine i want to configured one more instance of same web application as Staging
environment.
What are my options here to set ASPNETCORE_ENVIRONMENT
at application level instead of machine level? so i can host multiple instances of the same application on the same machine?
You have a couple of options.
Run each app as a different user, and set the environment variable within that user profile. This gives you a nice added security bonus. You'll have to set the app pool to load the user profile.
Use IIS configuration
- Start IIS manager
- Choose configuration editor Pull down the section combobox and choose system.webServer/aspNetCore
- Pull down the from combobox and choose Applicationhost.config
- Click on the environmentVariables element and click on the ... button hiding in the second column, at the right.
- Set your environment variables.
- Exit out of the environment variables screen and then click Apply.
- Restart the app pool/app.
Can you change the code parsing configuration running on the web server? That's what I would recommend doing. That would allow you to configure your environment more naturally in a Windows setting.
While the traditional way to configure the IHostingEnvironment.EnvironmentName
variable is via the ASPNETCORE_ENVIRONMENT
environment variable as you have done, you can change how ASP.NET Core parses its configuration such that you can set the variable via a command line argument.
To get into specifics...
By default, the Program.cs
file emitted by the dotnet new -t web
command looks something like the following:
public static void Main(string[] args) {
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://0.0.0.0:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
This makes ASP.NET Core use the default configuration processing (environment variables with a ASPNETCORE_
prefix) to determine the value of IHostingEnvironment.EnvironmentName
, which you are using to configure how your application runs.
Fortunately, you can alter the way that ASP.NET Core parses configuration by utilizing the UseConfiguration()
extension method on WebHostBuilder
. Here's an example of using custom configuration with the default implementation:
public static void Main(string[] args) {
var configuration =
new ConfigurationBuilder()
.AddEnvironmentVariables("ASPNETCORE_")
.Build();
var host =
new WebHostBuilder()
.UseConfiguration(configuration)
.UseKestrel()
.UseUrls("http://0.0.0.0:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
From here, I would change it so it can use the command line in addition to the ASPNETCORE_
prefixed environment variables. This will allow you to easily run your application with whatever environment name you want, like so:
public static void Main(string[] args) {
var configuration =
new ConfigurationBuilder()
.AddEnvironmentVariables("ASPNETCORE_")
.AddCommandLine(args)
.Build();
var host =
new WebHostBuilder()
.UseConfiguration(configuration)
.UseKestrel()
.UseUrls("http://0.0.0.0:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
Then, when you start your dotnet core application with dotnet run
, you can set the environment on the command line, like this:
dotnet run environment=development
dotnet run environment=staging
Now the ASPNETCORE_ENVIRONMENT
environment variable will still be respected, but you can override it via the command line when you are doing local development. As a note, you will need to include the Microsoft.Extensions.Configuration.CommandLine
nuget package to your project.json
file if you have no already done so to get the AddCommandLine()
extension method.
来源:https://stackoverflow.com/questions/40748518/how-to-configure-multiple-aspnetcore-environment-on-same-machine