如何在dotnet隔离(net5.0)azure函数中使用IOptions模式进行配置



我正试图将现有的Functions应用程序从core3.1 v3移植到net5.0 I,但不知道如何使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",

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

我遇到了同样的问题,但发现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"
}
}

最新更新