How to configure multiple ASPNETCORE_ENVIRONMENT on same machine?

被刻印的时光 ゝ 提交于 2019-11-30 03:25:41

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!