ASP.NET Core在构建过程中设置托管环境 [英] ASP.NET Core set hosting environment in build process

查看:89
本文介绍了ASP.NET Core在构建过程中设置托管环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET Core Api ,其中使用了appsettings.{environmentname} .json配置文件.然后,我还有一个适当的launchSettings.json文件,其中包含不同的环境选项,因此我可以使用任何特定的环境设置文件来运行.

在Startup.cs中,我们有一个条件设置,如果我们处于非产品环境中,则使用一组特定的 Jwt authentication (关闭了一些验证检查),然后在产品中加载了一个可以打开所有检查的版本.

在我的本地主机上,当environment.IsDevelopment()返回true,而environment.IsProduction()返回false时,此方法效果很好.太好了!

但是,当我在构建过程中运行并部署到测试环境时,该环境.IsDevelopment()现在返回false.

我已经在Program.cs文件的选项中添加了ConfigurationBuilder,以便可以将变量传递给构建过程,如下所示:

  dotnet恢复dotnet构建-环境开发"dotnet发布-o .. \ Artifacts 

我将发布相关文件以及相关代码,以获取更多信息...

Program.cs

 公共静态IWebHost BuildWebHost(string [] args){var config = new ConfigurationBuilder().AddCommandLine(参数).建造();返回WebHost.CreateDefaultBuilder(args).UseConfiguration(配置).UseStartup< Startup>().UseNLog().建造();} 

Startup.cs(ConfigureServices方法)

  services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>{//删除了不相关的代码...//options.TokenValidationParameters = Environment.IsProduction()//options.TokenValidationParameters = Environment.IsEnvironment("Prod")options.TokenValidationParameters = Environment.IsDevelopment()?devTokenValidationParameters:prodTokenValidationParameters;//options.TokenValidationParameters = devTokenValidationParameters;}); 

为什么是助手环境.{EnvironmentName}()检查在这里不起作用吗?

解决方案

环境名称是 runtime 概念,而不是 compile (或构建)时间概念.这意味着在构建(或发布)应用程序时,环境未知,设置无效.发布应用程序时,您的代码未运行.

您可以在运行应用程序时控制环境名称,例如通过 dotnet run :

  dotnet run --environment =生产 

或使用已知的环境变量 ASPNETCORE_ENVIRONMENT .例如,通过在命令行上执行以下操作:

  set ASPNETCORE_ENVIRONMENT =生产 

出于调试目的,也可以使用 launchSettings.json 文件设置此变量.使用Visual Studio或 dotnet new 创建新项目时,将生成此文件.

应用程序的默认环​​境为生产.请参阅文档以获取有关此主题的更多信息./p>

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?

解决方案

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.

这篇关于ASP.NET Core在构建过程中设置托管环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆