Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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:

Build definition

The ASPNETCORE_ENVIRONMENT environment variable is set in the Variables section of the build pipeline:

Build variables

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 in launchSettings.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.

UPDATE 1 (13/12/2019):

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.

like image 937
Rui Jarimba Avatar asked Nov 07 '22 11:11

Rui Jarimba


1 Answers

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}
like image 187
Viraj Patel Avatar answered Nov 12 '22 10:11

Viraj Patel