[英].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>
Click to see image below
enter image description here<\/a>点击查看下面的
图片 在此处输入图片描述<\/a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.