简体   繁体   English

新的 Visual Studio 2019 中的环境变量为空,但 Visual Studio 2017 中的环境变量为空

[英]Environment variables null in new Visual Studio 2019 but not Visual Studio 2017

I downloaded and setup the newly released Visual Studio 2019 Professional and opened a solution I have been working on in Visual Studio 2017 Professional.我下载并设置了新发布的 Visual Studio 2019 Professional,并打开了我在 Visual Studio 2017 Professional 中一直在研究的解决方案。

This solution contains 3 ASP.NET Core projects and 1 docker-compose project.此解决方案包含 3 个 ASP.NET Core 项目和 1 个 docker-compose 项目。

When starting debug session in 2019 I get a null reference exception on one line where I'm referring to my docker-compose environment variables.在 2019 年开始调试会话时,我在一行中收到了一个空引用异常,我指的是我的 docker-compose 环境变量。

However if I open the same solution in Visual Studio 2017 I don't get this null reference exception as the values are correctly read from the docker-compose.yml file.但是,如果我在 Visual Studio 2017 中打开相同的解决方案,我不会收到此空引用异常,因为这些值是从 docker-compose.yml 文件中正确读取的。

I have looked over the setup of both versions of Visual Studio and don't appear to be missing anything.我查看了两个版本的 Visual Studio 的设置,似乎没有遗漏任何东西。

// Create intance of item message handler
var container = services.BuildServiceProvider();
var _settings = container.GetService<IOptions<Settings>>().Value;

var itemListenerSettings = _settings.RabbitSettings.ListenerSettings.FirstOrDefault(s => s.Key == "ItemListener");
if (itemListenerSettings.Value != null)
{
  // Register item listener...
}
version: '3.4'

services:
  microserviceapi:
    image: microserviceapi
    container_name: ms
    build:
      context: ./MicroserviceApi
      dockerfile: Dockerfile
    environment:
      RabbitSettings:Hostname: "rabbit"
      RabbitSettings:Username: "user"
      RabbitSettings:Password: "pass"
      RabbitSettings:ReconnectionRetries: "10"
      RabbitSettings:ReconnectionTime: "1000"
      RabbitSettings:ListenerSettings__ItemListener__ExchangeName: "Imagine.ItemManagement"
      RabbitSettings:ListenerSettings__ItemListener__ExchangeType: "topic"
      RabbitSettings:ListenerSettings__ItemListener__QueueName: "foo.Item"
      RabbitSettings:ListenerSettings__ItemListener__RoutingKey: "item.*"

While running a debug session in Visual Studio 2019, calling var _settings = container.GetService<IOptions<Settings>>().Value I expect the property RabbitSettings of the variable _settings to contain the values from my docker-compose.yml file as it does in Visual Studio 2017.在 Visual Studio 2019 中运行调试会话时,调用var _settings = container.GetService<IOptions<Settings>>().Value我希望变量_settings的属性RabbitSettings包含来自我的 docker-compose.yml 文件的值在 Visual Studio 2017 中执行。

The environment variables contain the ':' characters.环境变量包含“:”字符。 As documented here:如此处所述:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#environment-variables-configuration-provider https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#environment-variables-configuration-provider

... it's recommended to use double-underscore '__' instead of ':' in enviroment variable names in asp.net core. ...建议在 asp.net core 的环境变量名称中使用双下划线 '__' 而不是 ':'。 ':' works fine in VS 2017 solutions, but VS2019 needs double-underscore '__'. ':' 在 VS 2017 解决方案中工作正常,但 VS2019 需要双下划线 '__'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM