Vladislav Qulin
Vladislav Qulin

Reputation: 1932

Environment variables configuration in .NET Core

I'm using the .NET Core 1.1 in my API and am struggling with a problem:

  1. I need to have two levels of configurations: appsettings.json and environment variables.
  2. I want to use the DI for my configurations via IOptions.
  3. I need environment variables to override 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

Answers (3)

Daniel
Daniel

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

Source

Upvotes: 31

Jacques Snyman
Jacques Snyman

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

user743414
user743414

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

  1. dev-final
  2. dev-local
  3. dev-test

And 4 *.json files

  • appsettings.json
  • appsettnigs.dev-final.json
  • appsettings.dev-local.json
  • appsettings.dev-test.json

appsettings.json holds global configuration values, and the other files specific ones.

Upvotes: 3

Related Questions