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"
}
}
}
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
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.
I have used the same configurations in
appsettings.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 theKUDU Console
–App Settings
.Configuration:
KUDU Console
appsettings.json
is also deployed.Path :
https://YourAppName.scm.azurewebsites.net/DebugConsole
Navigate to the KUDU Console, select
CMD
fromDebug Console
=>site
=>wwwroot
When I tried to retrieve the values from
appsettings.json
, you can see the values are displayed without any issue..cshtml
When you want to change the values of the Variables (
MyVariable1
andMyVariable2
) which you have set inappsettings.json
file, then you need to add the key-values in theConfiguration Section
of anAzure App Service
.The key name must be same as mentioned in the
appsettings.json
file.Application Settings:
Updated values from App Service: