Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure multiple ASPNETCORE_ENVIRONMENT on same machine?

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?

enter image description here

enter image description here

like image 768
LP13 Avatar asked Nov 22 '16 17:11

LP13


People also ask

Where is ASPNETCORE_ENVIRONMENT variable?

In Visual Studio, we can set ASPNETCORE_ENVIRONMENT in the debug tab of project properties. Open project properties by right clicking on the project in the solution explorer and select Properties. This will open properties page. Click on Debug tab and you will see Environment Variables as shown below.


2 Answers

You have a couple of options.

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

  2. Use IIS configuration

    1. Start IIS manager
    2. Choose configuration editor Pull down the section combobox and choose system.webServer/aspNetCore
    3. Pull down the from combobox and choose Applicationhost.config
    4. Click on the environmentVariables element and click on the ... button hiding in the second column, at the right.
    5. Set your environment variables.
    6. Exit out of the environment variables screen and then click Apply.
    7. Restart the app pool/app.
like image 127
blowdart Avatar answered Oct 21 '22 06:10

blowdart


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.

like image 41
Technetium Avatar answered Oct 21 '22 06:10

Technetium