I have a .NET Core build pipeline that is used to run some tests on 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:
The ASPNETCORE_ENVIRONMENT
environment variable is set in the Variables section of the build pipeline:
These builds are running on VMs that contains multiple build agents (private, not hosted).
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
. That value seems to come from the launchsettings.json
file of a project that is referenced by the tests project:
{
"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
:
launchSettings.json
is read if available.environmentVariables
settings inlaunchSettings.json
override environment variables.
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. 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.
I've tried setting a different value for ASPNETCORE_ENVIRONMENT
that is not set anywhere else:
{
"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:
System.IO.FileNotFoundException : The configuration file 'appsettings.Rui.base.json' was not found and is not optional.
I too faced the similar issue but in my case environment variables in "launchSettings.json" always were overriding other configurations.
I solved it by setting up multiple profiles in 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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With