gpt4 book ai didi

c# - 从 Azure Function App 读取嵌套配置

转载 作者:行者123 更新时间:2023-12-03 05:11:24 25 4
gpt4 key购买 nike

我在从 Azure Function(v6) 中的 local.settings.json 读取嵌套配置值时遇到问题,并且这些值似乎总是为 null。以下是我的代码片段:

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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2a7a1b7a0e392b6bdbfb3bbbcfcb1bdbf" rel="noreferrer noopener nofollow">[email protected]</a>"
},
"GetTaskDetails": {
"Url": "https://localhost:7000/api/Task/GetTaskDetails",
"Method": "GET",
"NotificationRecipients": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83f6f0e6f1b1c3e7eceee2eaedade0ecee" rel="noreferrer noopener nofollow">[email protected]</a>"
}
}
}
}

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"]
});
}
}
}

以下值始终为空。我哪里出错了?

                endpointDetailsConfig["Url"],
endpointDetailsConfig["Method"],
endpointDetailsConfig["NotificationRecipients"]

最佳答案

在您的 local.settings.json 文件中,"GetAllTask​​Details""GetTaskDetails" 存在于此处:

{
"Values": {
"EndpointDetails": {
"GetAllTaskDetails": {
...
},
"GetTaskDetails": {
...
}
}
}
}

即在这些路径:

"Values:EndpointDetails:GetAllTaskDetails"
"Values:EndpointDetails:GetTaskDetails"

通过设置

endpointDetailsListConfig = configuration.GetSection("EndpointDetails");

,您尝试定位不在配置根级别的部分。因此,GetSection() 的返回值不应该是实际的配置节。

根据 .GetSection() documentation :

This method will never return null. If no matching sub-section is found with the specified key, an empty IConfigurationSection will be returned.

旁注:
要验证某个部分(或值)是否存在,请调用 .Exists() .GetSection() 的返回值。

<小时/>

如果您更愿意定位“Values:EndpointDetails”,您应该能够从那里正确遍历树:

endpointDetailsListConfig = configuration.GetSection("Values:EndpointDetails");

关于c# - 从 Azure Function App 读取嵌套配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76636533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com