[英]Environment variables from launchsettings.json are being used in tests?
I have a .NET Core build pipeline that is used to run some tests on Azure DevOps.我有一个 .NET Core 构建管道,用于在 Azure DevOps 上运行一些测试。 Settings for each environment are stored in configuration files such as:
每个环境的设置都存储在配置文件中,例如:
appsettings.json
appsettings.qa.json
appsettings.test.json
Build is very basic - it contains a dotnet restore
, dotnet build
and dotnet test
tasks:构建是非常基础的——它包含一个
dotnet restore
、 dotnet build
和dotnet test
任务:
The ASPNETCORE_ENVIRONMENT
environment variable is set in the Variables section of the build pipeline: ASPNETCORE_ENVIRONMENT
环境变量在构建管道的变量部分设置:
These builds are running on VMs that contains multiple build agents (private, not hosted).这些构建在包含多个构建代理(私有,非托管)的 VM 上运行。
Now the weird part - sometimes the build is picking up the wrong settings!现在是奇怪的部分 -有时构建会选择错误的设置! .
.
After some investigation and adding more logging we realized that ASPNETCORE_ENVIRONMENT
value sometimes is Development-Selfhost
, instead of QA
.经过一些调查并添加更多日志记录后,我们意识到
ASPNETCORE_ENVIRONMENT
值有时是Development-Selfhost
,而不是QA
。 That value seems to come from the launchsettings.json
file of a project that is referenced by the tests project:该值似乎来自测试项目引用的项目的
launchsettings.json
文件:
{
"profiles": {
"MyProject.PublicApi": {
"commandName": "Project",
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development-Selfhost"
},
"applicationUrl": "http://localhost:5028/"
}
}
}
According to Use multiple environments in ASP.NET Core , launchSettings.json
file is used when an app is launched with dotnet run
:根据在 ASP.NET Core 中使用多个环境,使用
dotnet run
启动应用程序时使用launchSettings.json
文件:
launchSettings.json
is read if available.如果可用,则读取
launchSettings.json
。environmentVariables
settings inlaunchSettings.json
override environment variables.launchSettings.json
environmentVariables
设置会覆盖环境变量。
Adding the launchSettings.json
to .gitignore
solves my issue, but I'm trying to understand why it's using these settings if I don't execute a dotnet run
command in my build pipeline.将
launchSettings.json
添加到.gitignore
解决了我的问题,但我试图理解如果我不在我的构建管道中执行dotnet run
命令,它为什么使用这些设置。 Also, why is this behaviour so random?另外,为什么这种行为如此随意? Sometimes it uses the right settings, sometimes it doesn't.
有时它使用正确的设置,有时它没有。
I've checked the logs and I can confirm file launchSettings.json
is copied into the bin
folder of the test project.我检查了日志,可以确认文件
launchSettings.json
已复制到测试项目的bin
文件夹中。
I've tried setting a different value for ASPNETCORE_ENVIRONMENT
that is not set anywhere else:我尝试为
ASPNETCORE_ENVIRONMENT
设置一个不同的值,该值未在其他任何地方设置:
{
"profiles": {
"MyProject.PublicApi": {
"commandName": "Project",
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Rui"
},
"applicationUrl": "http://localhost:5028/"
}
}
}
Behaviour is basically the same - sometimes it uses the value set in the pipeline and other times it will use the value set in launchSettings.json
and will fail, because there is no correspondent configuration file:行为基本相同 - 有时它使用管道中设置的值,有时它会使用
launchSettings.json
设置的值并且会失败,因为没有对应的配置文件:
System.IO.FileNotFoundException : The configuration file 'appsettings.Rui.base.json' was not found and is not optional.
System.IO.FileNotFoundException :未找到配置文件“appsettings.Rui.base.json”,该文件不是可选的。
I too faced the similar issue but in my case environment variables in "launchSettings.json" always were overriding other configurations.我也遇到了类似的问题,但在我的情况下,“launchSettings.json”中的环境变量总是覆盖其他配置。
I solved it by setting up multiple profiles in launchSettings.json我通过在 launchSettings.json 中设置多个配置文件来解决它
{
"profiles": {
"grpc-server": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://0.0.0.0:5001"
},
"staging-profile": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
},
"applicationUrl": "http://0.0.0.0:5000"
},
"live-profile": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "http://0.0.0.0:80"
}
}
}
and passing the profile name with并传递配置文件名称
dotnet run --launch-profile {profile-name}
Looks like you have a test, maybe more than one, that is starting and running an application.看起来您有一个测试,可能不止一个,正在启动和运行应用程序。 That would cause launch settings to be read and environment variables to be set durring the dotnet test task.
这将导致在 dotnet 测试任务期间读取启动设置并设置环境变量。 That would explain:
那将解释:
System.IO.FileNotFoundException : The configuration file 'appsettings.Rui.base.json' was not found and is not optional.
System.IO.FileNotFoundException :未找到配置文件“appsettings.Rui.base.json”,该文件不是可选的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.