简体   繁体   English

".NET Core 控制台应用程序和 aspnetcore_environment 变量"

[英].NET Core console app and aspnetcore_environment variable

I have a .net core CONSOLE app.我有一个 .net 核心控制台应用程序。 In the code I am reading in the appropriate environment specific appsettings.dev.json, appsettings.uat.json etc.在我正在阅读的代码中,特定于 appsettings.dev.json、appsettings.uat.json 等的适当环境。

  var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
  configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
                .AddJsonFile("appsettings.json", false)
                .AddJsonFile($"appsettings.{environmentName}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();

When this is run directly in Visual Studio the ASPNETCORE_ENVIRONMENT variable is set from the launchSettings.json file or from amending the project properties, debug tab and the correct appsettings.environment.json file is read.当它直接在 Visual Studio 中运行时,ASPNETCORE_ENVIRONMENT 变量是从 launchSettings.json 文件或通过修改项目属性、调试选项卡设置的,并读取正确的 appsettings.environment.json 文件。

When i want to publish the project via Visual Studio ie right click project and publish (ie I do not want to do this via the command line) I select the appropriate .pubxml file held within PublishProfile folder.当我想通过 Visual Studio 发布项目时,即右键单击项目并发布(即我不想通过命令行执行此操作),我选择保存在 PublishProfile 文件夹中的适当 .pubxml 文件。 In this .pubxml file I have set the environmentName.在这个 .pubxml 文件中,我设置了 environmentName。

  <EnvironmentName>dev</EnvironmentName>

However, when running the exe that is produced by the publish process - the environmentName variable specified above is always blank and consequently it doesn't pick up the correct environmentName setting and hence the appropriate appsettings.environment.json file is not read in.但是,在运行发布过程生成的 exe 时 - 上面指定的 environmentName 变量始终为空,因此它不会选择正确的 environmentName 设置,因此不会读入适当的 appsettings.environment.json 文件。

当您想运行已发布的 .exe 文件时,请在命令行(powershell)中尝试这种方式:

$env:ASPNETCORE_ENVIRONMENT="dev";yourfile.exe

I discovered that if I change the actual Machine environment variable (ASPNETCORE_ENVIRONMENT), the deployed application adjust accordingly.我发现如果我更改实际的机器环境变量 (ASPNETCORE_ENVIRONMENT),部署的应用程序会相应地进行调整。

Remember!记住! (This application is already published - Its behavior changes as I update the variable on the box and restart the application) (此应用程序已经发布 - 当我更新盒子上的变量并重新启动应用程序时,它的行为会发生变化)

Click to see image below点击查看下图

enter image description here<\/a>在此处输入图像描述<\/a>

ASPNETCORE_ENVIRONMENT<\/strong> : When it's set to Production my application uses the appsettings.production.json file ASPNETCORE_ENVIRONMENT<\/strong> :当它设置为生产时,我的应用程序使用 appsettings.production.json 文件

ASPNETCORE_ENVIRONMENT<\/strong> : When it's set to Development my application uses the appsettings.Development.json file ASPNETCORE_ENVIRONMENT<\/strong> :当它设置为开发时,我的应用程序使用 appsettings.Development.json 文件

Click to see image below enter image description here<\/a>点击查看下面的图片 在此处输入图片描述<\/a>

I also discovered that I actually had to restart my Visual Studio 2019 in order for the newly added environment variable to take affect.我还发现实际上我必须重新启动 Visual Studio 2019 才能使新添加的环境变量生效。 Basically, I believe the environment variables are being cached some how behind the scenes.基本上,我相信环境变量在幕后被缓存了一些。 Therefore, if you do add another variable you may have to restart Visual Studio in order for them to take affect.因此,如果您确实添加了另一个变量,您可能必须重新启动 Visual Studio 才能使它们生效。 ... Hummmm ...嗯

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net Core 2.1 ASPNETCORE_ENVIRONMENT值未反映 - ASP.Net Core 2.1 ASPNETCORE_ENVIRONMENT value not reflecting 如何为控制台应用程序设置 ASPNETCORE_ENVIRONMENT? - How to set ASPNETCORE_ENVIRONMENT for console application? 如何在运行时设置 ASPNETCORE_ENVIRONMENT 变量 - How to set ASPNETCORE_ENVIRONMENT variable at runtime 是否可以重命名 ASPNETCORE_ENVIRONMENT 变量? - Is it possible to rename ASPNETCORE_ENVIRONMENT variable? 如何设置要考虑发布 ASP.NET Core 应用程序的 AS.NETCORE_ENVIRONMENT - How to set ASPNETCORE_ENVIRONMENT to be considered for publishing an ASP.NET Core application .core mvc发布的应用程序局部视图在ASPNETCORE_ENVIRONMENT“生产”中不起作用,但在“开发”中工作良好 - .Core mvc published app partial views doesnt work in ASPNETCORE_ENVIRONMENT “production” but working fine with “development” ASP.NET Core 3.1 - ASPNETCORE_ENVIRONMENT 的“开发”值在启动期间触发其他服务的生命周期验证 - ASP.NET Core 3.1 - `Development` value for ASPNETCORE_ENVIRONMENT triggers additional services' lifetimes validation during the startup 在T4模板中获取ASPNETCORE_ENVIRONMENT? - Get ASPNETCORE_ENVIRONMENT in T4 Template? 创建新的ASPNETCORE_ENVIRONMENT进行开发 - Create new ASPNETCORE_ENVIRONMENT for Development 在构建配置中设置aspnetcore_environment - Set aspnetcore_environment within build configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM