ASP.NET Core set hosting environment in build process

喜欢而已 提交于 2021-01-28 01:24:06

问题


I have an ASP.NET Core Api where I use the appsettings.{environmentname}.json configuration files. Then I also have the appropriate launchSettings.json file with the different environment options so I can run with any specific environment settings file.

In the Startup.cs, we have a conditional setting where if we are in a non-prod environment, then we use a specific set of Jwt authentication (just has some validating checks turned off), then in prod, we load a different version that has all of the checks to turn on.

On my localhost, this works great where environment.IsDevelopment() returns true, while environment.IsProduction() returns false. Great!

But, when I run this through our build process and deploy to our test environment, the environment.IsDevelopment() now returns false.

I have added in the option in the Program.cs file to add the ConfigurationBuilder so I can pass variables to my build process, which looks like this:

dotnet restore
dotnet build --environment "Development"
dotnet publish -o ..\Artifacts

I'll post the relevant files, and the associated code for more info...

Program.cs

public static IWebHost BuildWebHost(string[] args)
{
    var config = new ConfigurationBuilder()
        .AddCommandLine(args)
        .Build();

    return WebHost.CreateDefaultBuilder(args)
        .UseConfiguration(config)
        .UseStartup<Startup>()
        .UseNLog()
        .Build();
}

Startup.cs (ConfigureServices method)

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
    // removed code not relevant...


    // options.TokenValidationParameters = Environment.IsProduction()
    // options.TokenValidationParameters = Environment.IsEnvironment("Prod")
    options.TokenValidationParameters = Environment.IsDevelopment()
        ? devTokenValidationParameters
        : prodTokenValidationParameters;

    // options.TokenValidationParameters = devTokenValidationParameters;
});

Why are the helper environment.Is{EnvironmentName}() checks not working here?


回答1:


The environment name is runtime concept rather than a compile (or build) time concept. This means that when building (or publishing) an application the environment is not yet known and setting is has no effect. Your code is not running when you publish the application.

You can control the environment name when running the application e.g. via an argument of dotnet run:

dotnet run --environment=Production

Or using a known environment variable ASPNETCORE_ENVIRONMENT. For example by executing this at the command line:

set ASPNETCORE_ENVIRONMENT=Production

This variable might also be set using the launchSettings.json file for debugging purposes. This file is generated when creating a new project using Visual Studio or dotnet new.

The default environment for an application is Production. Please refer to the documentation for more info about this topic.



来源:https://stackoverflow.com/questions/50821504/asp-net-core-set-hosting-environment-in-build-process

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