I'm facing issues in reading Nested configuration values from local.settings.json in my Azure Function(v6) and the values always seem to be coming as null. Below are my code snippets:
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"EndpointDetails": {
"GetAllTaskDetails": {
"Url": "https://localhost:7000/api/Task/GetAllTaskDetails",
"Method": "GET",
"NotificationRecipients": "user1@domain.com"
},
"GetTaskDetails": {
"Url": "https://localhost:7000/api/Task/GetTaskDetails",
"Method": "GET",
"NotificationRecipients": "user2@domain.com"
}
}
}
}
Startup.cs:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(HealthChecker.Startup))]
namespace HealthChecker
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
// Getting the IConfiguration instance
var configuration = builder.GetContext().Configuration;
// Registering health checks
builder.Services.AddHealthChecks();
// Calling the method that defines health checks
HealthCheckDefinitions.ConfigureHealthChecks(builder.Services.AddHealthChecks(), configuration);
}
}
}
HealthCheckDefinitions.cs:
public static class HealthCheckDefinitions
{
private static Dictionary<string, List<string>> endpointDetails = new Dictionary<string, List<string>>();
public static void ConfigureHealthChecks(IHealthChecksBuilder builder, IConfiguration configuration){
private static IConfigurationSection endpointDetailsListConfig;
private static string getAllTaskDetails;
private static string getTaskDetails;
List<string> endpoints = new List<string>();
InitializeConfigValues(configuration);
endpoints.AddRange(new List<string>
{
getAllTaskDetails,
getTaskDetails
});
foreach (var endpoint in endpoints)
{
var endpointDetailsConfig
= endpointDetailsListConfig.GetSection(endpoint);
GetEndpointDetails(endpointDetailsConfig, endpoint);
foreach (var endpointDetail in endpointDetails)
{
builder.AddCheck(endpointDetail.Key, () =>
{
// lines of code
// .....
});
}
}
}
private static void InitializeConfigValues(IConfiguration configuration)
{
endpointDetailsListConfig = configuration.GetSection("EndpointDetails");
getAllTaskDetails = endpointDetailsListConfig.GetSection("GetAllTaskDetails").Key;
getTaskDetails = endpointDetailsListConfig.GetSection("GetTaskDetails").Key;
}
private static void GetEndpointDetails(IConfigurationSection endpointDetailsConfig, string endpoint)
{
if (!string.IsNullOrEmpty(endpointDetailsConfig["Url"]) && !string.IsNullOrEmpty(endpointDetailsConfig["Method"])
&& !string.IsNullOrEmpty(endpointDetailsConfig["NotificationRecipients"]))
{
endpointDetails.Add(endpoint, new List<string>
{
endpointDetailsConfig["Url"],
endpointDetailsConfig["Method"],
endpointDetailsConfig["NotificationRecipients"]
});
}
}
}
The values of the following are always null. Where am I going wrong here?
endpointDetailsConfig["Url"],
endpointDetailsConfig["Method"],
endpointDetailsConfig["NotificationRecipients"]