首页 > 解决方案 > 如何在 dotnet-isolated (net5.0) azure 函数中使用 IOptions 模式进行配置

问题描述

我正在尝试将现有的 Functions 应用程序从 core3.1 v3 移植到 net5.0,但不知道如何使 IOptions 配置模式正常工作。

我的配置local.settings.json存在于配置数据中,我可以使用 GetEnvironmentVariable 来获取它。尽管如此,以下内容并没有像以前那样将值绑定到 IOptions 配置。

.Services.AddOptions<GraphApiOptions>()
    .Configure<IConfiguration>((settings, configuration) => configuration.GetSection("GraphApi").Bind(settings))

这些值与local.settings.json以前一样:

    "GraphApi:AuthenticationEndPoint": "https://login.microsoftonline.com/",
    "GraphApi:ClientId": "316f9726-0ec9-4ca5-8d04-f39966bebfe1",
    "GraphApi:ClientSecret": "VJ7qbWF-ek_Amb_e747nXW-fMOX-~6b8Y6",
    "GraphApi:EndPoint": "https://graph.microsoft.com/",
    "GraphApi:TenantId": "NuLicense.onmicrosoft.com",

这仍然支持吗?我错过了什么?

标签: configurationazure-functions.net-5

解决方案


我有同样的问题,但结果是json 格式不正确

仅供参考,这是我的配置方式:

var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices(s =>
            {
                s.AddOptions<ApplicationSettings>().Configure<IConfiguration>((settings, configuration) =>
                {
                    configuration.GetSection(nameof(ApplicationSettings)).Bind(settings);
                });
            })
            .Build();

以下是 local.setting.json 的示例:

{
  "IsEncrypted": false,
  "Values": {
    "ApplicationSettings:test": "testtest",
    "ApplicationSettings:testtest": "test"
  }
}

推荐阅读