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;
}
}
}