10

I'm working with a C# class that reads from appsettings using

Environment.GetEnvironmentVariable("setting")

I want to make another console program (.net core 3.0) that reads settings from appsettings.json and loads them into environment variables.

static void Main(string[] args)
{
     var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddEnvironmentVariables();

            IConfiguration config = builder
                .Build();

    var businessLogic = new BusinessLogic(); // uses GetEnvironmentVariable to get configuration
}

In my business logic class, the environment variables aren't getting pulled back correctly. What might be the problem?

appsettings.json is in this format:

{
    "SETTING" : "setting value"
}
Simply Ged
  • 8,250
  • 11
  • 32
  • 40
Benny
  • 186
  • 1
  • 1
  • 11

2 Answers2

6

Environment.GetEnvironmentVariable("setting") will always try to read the data from an environment variable, not from your loaded config.

You need to change your BusinessLogic class to accept the config object you built and use that to access the settings. An example:

public class BusinessLogic
{
    public BusinessLogic(IConfiguration config)
    {
        var value = config.GetValue<string>("SETTING");
    }
}

In this example value will contain the correct value of SETTING if it was last set in appsettings.json or using an environment variable.

Simply Ged
  • 8,250
  • 11
  • 32
  • 40
0

First of all appsettings file and environment variables are different things.

Environment variables can be set before the application starts so they can be used to configure the way application starts. However, appsettings are files usually inside the project folder and are loaded after the program starts. They sometimes might be utilized in place of one another, however, while almost always it is possible to utilize environment variables instead of appsettings it might not be possible to do vice versa.

The main idea behind environment variables and appsettings is that you can make changes in the application without changing the source code for example utilizing this feature will eliminate the need for building different source codes for different environments.

In dot net core environment variables are read like Environment.GetEnvironmentVariable("DbConnection") and can be set from source code like Environment.SetEnvironmentVariable("IsActive", "true");

The appsettings file is read like you did

var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json")

After this usually the appsettings class is injected and utilized by services in the program like

builder.Services.Configure<Appsettings>(configuration.GetSection("Appsettings"))

there is no use case that I am aware of for what you asked, however, you can load the appsettings and then add it to environment variables like below

var configuration = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("appsettings.json", false)
               .Build();

var appsettings = configuration.GetSection("Appsettings").Get<Appsettings>();
Environment.SetEnvironmentVariable(nameof(appsettings.AzureWebJobsStorage), appsettings.AzureWebJobsStorage);
            

This code reads the appsettings file and then adds the AzureWebJobsStorage variable into environment variable. That can be read and accessed anywhere in the code using Environment.GetEnvironmentVariable("AzureWebJobsStorage")

However, I believe what you are really looking for is the code below:

var configuration = new ConfigurationBuilder()
   .SetBasePath(Directory.GetCurrentDirectory())
   .AddJsonFile("appsettings.json", false)
   .AddEnvironmentVariables()
   .Build();

builder.Services
    .Configure<Appsettings>(configuration.GetSection("Appsettings"));


And then you can access the appsettings variables in the services like

namespace Setur.ShortMessageService
{
    public class SendSms
    {
        private readonly Appsettings _appsettings;
        public SendSms(IOptions<Appsettings> options)
        {
            _appsettings = options.Value;
        }
    }
}
Tekin
  • 554
  • 7
  • 17