Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 3.1 application not finding environment variable

I cannot get my .NET Core 3.1 console application to recognize my custom system environment variables. I can pull and print other system variables such as username just fine. But if I set a custom one like 'TestKey' in the example below, they are always null within the application.

Here is my code:

static void Main()
    {
        var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();
        var value = config.GetValue<string>("TestKey");
        var envValue = Environment.GetEnvironmentVariable("TestKey");
        Console.WriteLine($"Config Variable is: {value}");
        Console.WriteLine($"Environment Variable: {envValue}");
    }

This is the output:

console output

and system variables clearly show the variable has been set:

system variable screenshot

Likewise, when I do an echo from the command prompt for the TestKey variable, it correctly returns the associated value.

command prompt output

This is a Windows 10 Pro computer and it is not part of a domain. This behavior is truly puzzling. I have rebooted the computer and the system variable persist but still will not appear in the application.

like image 334
cygnim Avatar asked Apr 11 '20 17:04

cygnim


2 Answers

There are two major points here. First, according to Configuration in ASP.NET Core, environment variables are retrieved only for current process

Are only set in processes launched from the command window they were set in

So, you should set them in the same command window before launch the app or add to the launchsettings.json file

"environmentVariables": {
  "Test": "Test",
  "ASPNETCORE_ENVIRONMENT": "Development"
}

and restart Visual Studio for that. Or just change it in Debug properties of your project

enter image description here

Second, to be able to get a system environment variable in GetEnvironmentVariable method, you should specify the EnvironmentVariableTarget.Machine parameter

var envValue = Environment.GetEnvironmentVariable("Test", EnvironmentVariableTarget.Machine);

By default

The GetEnvironmentVariable(String) method retrieves an environment variable from the environment block of the current process only

Or, if you'll add the variable to launchsettings.json in previous point, setting the target isn't needed.

However, if you will run your project in command line via dotnet run, you should be able to access the system environment variable as well, because

On Windows systems, the environment block of the current process includes:

  • All environment variables that are provided to it by the parent process that created it. For example, a .NET application launched from a console window inherits all of the console window's environment variables.

  • If there is no parent process, per-machine and per-user environment variables are used instead. For example, a new console window has all per-machine and per-user environment variables defined at the time it was launched.

like image 160
Pavel Anikhouski Avatar answered Sep 28 '22 17:09

Pavel Anikhouski


Bottom Line: I had to reboot my web server.

I had this very same problem, in a razor view i had this...

@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment env
.
.
<div>
    Environment is @env.EnvironmentName
</div>
<environment include="Development">
    <strong>You are in the Development environment</strong>
</environment>

and in ASPNETCORE_ENVIRONMENT variable i had 'Development'

The page never saw 'Development' it always defaulted to 'Production' (which it's supposed to). Rebooting the web server 'fixed' it for me. I'm not sure if it was the reboot or I just could have recycled the app pool. I'm using .net core 3.1

like image 22
RobStone1 Avatar answered Sep 28 '22 19:09

RobStone1