Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Application Settings in Azure on a WebAPI in .NetCore 2.0

I have a WebAPI that I am publishing it on Azure. I am using .Net Core 2.0 on my application.

I have a file called appsettings.json which has the following configuration:

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
          "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "AppConfiguration": {
    "MyVariable": false,
  }
...
}

In my application, running in localhost, I can get the value from "MyVariable".

When I publish it to Azure, I can also get the value from "MyVariable".

However, when I go to the Application Settings of my Azure application, I set "MyVariable" there to "true", but my application keeps getting the value "false".

To summarise, I am not able to get the value from Azure, only from the appsettings.json.

I tried using, on Azure, the following key-value:

AppConfiguration:MyVariable - true

MyVariable - true

None of them worked.

Can anyone help me in how to get the value from the Azure Application Settings?

Edit 1 My program class:

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
               .ConfigureAppConfiguration(
                    (WebHostBuilderContext context, IConfigurationBuilder builder) =>
                    {
                        builder.Sources.Clear();

                        builder
                            .AddEnvironmentVariables()
                            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                    })
            .UseApplicationInsights()
            .UseStartup<Startup>()
            .Build();
like image 370
Artur Quirino Avatar asked Dec 05 '25 04:12

Artur Quirino


2 Answers

The order of the settings sources matters. Change this:

builder
    .AddEnvironmentVariables()
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

to this:

builder
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables();

to have the settings from the Azure environment override the ones in the settings file.

like image 143
Rytmis Avatar answered Dec 07 '25 20:12

Rytmis


In Net Core 2.o or superior, the order matters. if you check here you can see the order:

A typical sequence of configuration providers is:

  1. Files (appsettings.json, appsettings..json, where is the app's current hosting environment)
  2. User secrets (Secret Manager) (in the Development environment only)
  3. Environment variables
  4. Command-line arguments

So, you must change .AddEnvironmentVariables() to this:

 builder
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
 .AddEnvironmentVariables();
like image 35
ArlanG Avatar answered Dec 07 '25 18:12

ArlanG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!