简体   繁体   English

来自 launchsettings.json 的环境变量正在测试中使用吗?

[英]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 restoredotnet builddotnet 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 in launchSettings.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.有时它使用正确的设置,有时它没有。

UPDATE 1 (13/12/2019):更新 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.我检查了日志,可以确认文件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:那将解释:

  1. How your environement variables are being overriden您的环境变量如何被覆盖
  2. How the following exception was thrown以下异常是如何抛出的

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.

相关问题 如何在 linux 上的 launchsettings.json 中引用环境变量 - How to reference environment variables in launchsettings.json on linux 运行单元测试时是否可以加载来自 launchSettings.json 文件的配置文件? - Can a profile from the launchSettings.json file be loaded when running unit tests? 在launchSettings.json中引用环境变量的值 - Referring to environment variable's value inside launchSettings.json 从Mac上的命令行运行ASP.NET 5应用程序时是使用launchSettings.json吗? - Is launchSettings.json used when running ASP.NET 5 apps from the command line on Mac? launchsettings.json 是否在生产中使用的 dotnet 核心项目中? - Is launchsettings.json in a dotnet core project used in production? 如何从 launchsettings.json 获取当前应用程序 url - How to get current application url from launchsettings.json Visual Studio for Mac 忽略launchSettings.json - Visual Studio for Mac Ignoring launchSettings.json 微服务未在 LaunchSettings.JSON 设置的端口上启动 - Microservice not Launching on Port Set by LaunchSettings.JSON Launchsettings.json 不是使用 dotnet new 创建的 - Launchsettings.json not created using dotnet new launchSettings.json 文件打开错误的 URL - launchSettings.json file opens wrong URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM