3

I am building a Web API using dotnet core 3.1 and now I got into the need to store a kind of sensitive information on a Windows Environment variable which I already did. Let's call it MY_WIN_VAL_X.

I tried to use the following code on the ConfigureServices method on the Startup.cs (link)

public class Startup
{
    private IHostEnvironment Environment { get; set; }

    public Startup(IConfiguration configuration, IHostEnvironment environment)
    {
        Environment = environment;

        this.Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
       ...other code
       var myEnvValX = Environment.GetEnvironmentVariable("MY_WIN_VAL_X");
    }
}

However, I got this error:

'IHostEnvironment' does not contain a definition for 'GetEnvironmentVariable' and no accessible extension method 'GetEnvironmentVariable' accepting a first argument of type 'IHostEnvironment' could be found (are you missing a using directive or an assembly reference?) [API]csharp(CS1061)

Does anyone knows how to achieve it? How to get the custom Windows environment variable?

1 Answer 1

3

Reading the environment variables is done via the Configuration mechanism, and therefore are accessible via the IConfiguration object rather than the IHostEnvironment object.

To add the environment variables to your configuration, you'll need to add this to your Program.cs file:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddEnvironmentVariables(); // <-- this line
                // optional: use a prefix
                //config.AddEnvironmentVariables(prefix: "MYPREFIX_"); // <-- this line
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

And then you can read your environment variable like this:

public class Startup
{
    private IConfiguration Configuration { get; set; }
    private IHostEnvironment Environment { get; set; }

    public Startup(IConfiguration configuration, IHostEnvironment environment)
    {
        Environment = environment;

        this.Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
       ...other code
       var myEnvValX = this.Configuration["MY_WIN_VAL_X"];
    }
}

Note that you can restrict which environment variables are available by prefixing them. For example, if you have MYPREFIX_MY_WIN_VAL_X, MYPREFIX_MY_WIN_VAL_Y, you can restrict the env variables with config.AddEnvironmentVariables(prefix: "MYPREFIX_"), and only those two variables will be exposed (the prefix will be removed).

Not the answer you're looking for? Browse other questions tagged or ask your own question.