skip to Main Content

I am not able to see on my Azure App Service Configuration (or accessing by Kudus), variables I set in appSettings.json files in the program file.

Example of appsettings.json on the .NET 7 project:

{
  "MyVariable1": "Hello",
  "MyVariable2": "World!",
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "Microsoft.AspNetCore": "Debug"
    }
  }
}

Image from Kudus

I’m not able to see "MyVariable1", "MyVariable2" in AppSettings.

I’m working on a web application on .NET 7.0, then in the program.cs file I’m reading the appsettings.json file like this:

// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Attempt #1 - Not working
builder.Configuration    
    .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings{environment}.json", optional: true, reloadOnChange: true)    
    .AddEnvironmentVariables();

// Attempt #2 - Not working
string? baseDirectory = Directory.GetCurrentDirectory();
builder.Configuration
    .SetBasePath(baseDirectory)
    .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true)
    .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

// Attempt #3 - Not working
string? baseDirectory = AppContext.BaseDirectory;
builder.Configuration
    .SetBasePath(baseDirectory)
    .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true)
    .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

// Attempt #4 - Not working
string? baseDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly()?.Location);
builder.Configuration
    .SetBasePath(baseDirectory)
    .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true)
    .AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()

BTW, I have already set "Copy Always" option for every single appsettings file. Anyways, it does not work.

<ItemGroup>
    <None Include="...editorconfig" Link=".editorconfig" />
    <None Include="appsettings.Development.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Include="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Include="appsettings.production.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Include="appsettings.Staging.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

Does anybody knows what I’m missing about this?

2

Answers


  1. That’s normal.

    Much like you have added 3 sources of configuration in your attempt one, many sources can be added and appsettings.json is just one of them.

    The values you configure directly in the App Service Configuration are another. The online editor (and Kudu as you are using it) is only concerned with these and hence only showing them, but all sources added are available to your running code.

    Note that WebApplication.CreateBuilder(args) is already doing what you are trying to do with the various attempts in your question, by default, so that code is not needed in most scenarios.

    See the docs on this for more details.

    Login or Signup to reply.
  2. I have used the same configurations inappsettings.json file which you have provided and deployed the App to Azure App Service.

    You can see the values are neither available in the Configure Section nor in the KUDU ConsoleApp Settings.

    Configuration:
    enter image description here

    KUDU Console
    enter image description here

    • You can see the appsettings.json is also deployed.
      Path :
      https://YourAppName.scm.azurewebsites.net/DebugConsole

    Navigate to the KUDU Console, select CMD from Debug Console => site => wwwroot

    enter image description here

    When I tried to retrieve the values from appsettings.json, you can see the values are displayed without any issue.

    enter image description here

    .cshtml

    @using Microsoft.Extensions.Configuration
    @inject IConfiguration myconfig
    <div class="text-center">
    <h2>@myconfig["MyVariable1"]</h2>  <h2> @myconfig["MyVariable2"]</h2> 
    </div>
    
    • When you want to change the values of the Variables (MyVariable1 and
      MyVariable2) which you have set in appsettings.json file, then you need to add the key-values in the Configuration Section of an Azure App Service.

    • The key name must be same as mentioned in the appsettings.json file.

    Application Settings:

    enter image description here

    Updated values from App Service:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search