C# Console application on Linux and Environment variable

188 Views Asked by At

I have a C# console application that I compiled for Linux. The application needs to access environment variables. I tried setting it up using EXPORT var1=value1 or by setting it in /etc/environment. I see the environment variable is set correctly when I do printenv, but when my application runs it can't access the variable.

Can someone suggest how to make my application access environment variables? I am running it on RHEL 8 and in my application, I am accessing environment variable using Environment.GetEnvironmentVariable

Here is a code to demonstrate it:

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    while (!stoppingToken.IsCancellationRequested)
    {
        var env = Environment.GetEnvironmentVariable("HOMEBREW_PREFIX");

        Console.WriteLine($"In worker process: {env}");
        _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
        await Task.Delay(1000, stoppingToken);
    }
}

Here is a partial output from printenv that shows environment variable called HOMEBREW_PREFIX set correctly.

HOMEBREW_PREFIX=/opt/homebrew
HOMEBREW_CELLAR=/opt/homebrew/Cellar

Here is screenshot at the breakpoint and my watch window:

enter image description here

This happens on both my mac and my RHEL box. Environment.GetEnvironmentVariable() return null and can't access environment variables.

There is yet another question that seems to be doing similar things, but I am trying to access an environment variable which already exists, while the other one is trying to set it.

How can I fix this?

4

There are 4 best solutions below

1
Sasisa Play On

Try using version of this method, which supports target as the second argument. It can be, that these environment variables are not visible to process target.

3
Joel R. Hall On

In looking through the online docs for Environment.GetEnvironmentVariable, I noticed the following under the Remarks heading:

The GetEnvironmentVariable(String) method retrieves an environment variable from the environment block of the current process only. It is equivalent to calling the GetEnvironmentVariable(String, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget.Process.

To retrieve all environment variables along with their values, call the GetEnvironmentVariables method.

Try switching to that method to see if that solves your problem.

1
Joel Coehoorn On

As is often the case, its worth reading the docs for the method. When we do, we see this excerpt:

On non-Windows systems, the GetEnvironmentVariables method retrieves the name and value of all environment variables that are inherited from the parent process that launched the dotnet process or that are defined within the scope of the dotnet process itself. Once the dotnet process ends, these latter environment variables cease to exist.

.NET running on Unix-based systems does not support per-machine or per-user environment variables.

(emphasis mine)

0
Mario Villanueva On

It is necessary to set environment variables in the same thread where .NET Core is running and use SetEnvironmentVariable.

enter image description here

In the image, if you set ENV_PATH_CONFIG in the terminal, when you run your code, it's not possible to get ENV_PATH_CONFIG. However, if you set it before retrieving it, you can view the value.

Is more easy if you set launchSettings.json file.