Reputation: 1932
I'm using the .NET Core 1.1 in my API and am struggling with a problem:
appsettings.json
and environment variables.IOptions
.appsettings.json
values.So I do it like this so far:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Something here
services.Configure<ConnectionConfiguration>(options =>
Configuration.GetSection("ConnectionStrings").Bind(options));
// Something there
}
With my appsettings.json
composed like this
{
"ConnectionStrings": {
"ElasticSearchUrl": "http://localhost:4200",
"ElasticSearchIndexName": "myindex",
"PgSqlConnectionString": "blablabla"
}
}
I get all the configurations mapped to my class ConnectionConfiguration.cs
. But I cannot get the environment variables to be mapped as well. I tried the names like: ConnectionStrings:ElasticSearchUrl
, ElasticSearchUrl
, even tried specifying the prefix to .AddEnvironmentVariables("ConnectionStrings")
without any result.
How should I name the environment variables so it can be mapped with services.Configure<TConfiguration>()
?
Upvotes: 23
Views: 43496
Reputation: 1632
The
:
separator doesn't work with environment variable hierarchical keys on all platforms.__
, the double underscore, is supported by all platforms and it is automatically replaced by a:
Try to name the environment variable like so ConnectionStrings__ElasticSearchUrl
Upvotes: 31
Reputation: 4320
Have a look at ASP.NET Core Configuration with Environment Variables in IIS
To do the same with environment variables, you just separate the levels with a colon (:), such as
HitchhikersGuideToTheGalaxy:MeaningOfLife
.
In your case you'll use ConnectionStrings:ElasticSearchUrl
as mentioned in your question.
Also notice the following:
When you deploy to IIS, however, things get a little trickier, and unfortunately the documentation on using configuration with IIS is lacking. You go into your server's system settings and configure all your environment variables. Then, you deploy your app to IIS, and it... explodes, because it's missing those necessary settings.
Turns out, in order to use environment variables with IIS, you need to edit the advanced settings for your App Pool. There, you'll find a setting called "Load User Profile". Set that to True. Then, recycle the App Pool to load in the environment variables. Note: you must do this even if your environment variables are added as "System variables", rather than "User variables".
Upvotes: 7
Reputation: 936
I believe you're looking for this:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
I've 3 configurations
And 4 *.json files
appsettings.json holds global configuration values, and the other files specific ones.
Upvotes: 3