'Visual Studio 2022 System.Environment.GetEnvironmentVariable not working

I know this is still in preview, but I just want to make sure I am not doing anything wrong as I have done things like this in the past. I have my Environment variables set in properties: enter image description here

And I am trying to set up my tests:

[TestInitialize]
public void Initialize()
{
    var test = Environment.GetEnvironmentVariables();
    // test enumerates all the Env variables, don't see it there
    var connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING");
    if (string.IsNullOrWhiteSpace(connectionString)) // so this is obviously null
        throw new ArgumentNullException("CONNECTION_STRING");
    _ConnectionString = connectionString;
}

As you can see by my comments, the environment variables are not found/loaded.

What am I missing? Thank you.



Solution 1:[1]

In Visual Studio 2022, you can access environment variables in development by modifying the following section of launchSettings.json file.

"environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "VaultUri": "https://xxx.vault.azure.net/", "AZURE_USERNAME": "[email protected]" }

where VaultUri is the name of your environment variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1