尝试从门户中创建的 Azure Function 访问应用程序设置。
当前未找到 ConfgiurationBuilder 命名空间的问题。
我最终得到了以下代码,对于应该鼓励使用的代码来说,这似乎有点冗长。
到目前为止,我发现的大多数资源都引用了 Visual Studio 使用启动绑定(bind)构建的 Azure 函数。
net core c# 函数。 版本2功能。
首先 - 功能应用中的设置设置平台设置/常规设置/配置
然后是函数代码:
- System.Configuration.ConfigurationManager 的软件包
- 使用 Microsoft.Extensions.Configuration 添加;
- 添加 ExectuionContext 上下文作为参数
代码如下所示
#r "Newtonsoft.Json"
#r "System.Configuration.ConfigurationManager"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Extensions.Configuration;
public static async Task<string> Run(HttpRequest req, ILogger log,
ExecutionContext context) {
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var app_setting = config["MyAppSetting"];
return app_setting;
}
正如前面提到的问题
- 由于未找到 ConfigurationBuilder 命名空间而无法构建,尽管 #r ConfigurtaionManager 没有出现错误
- 看起来有点长。如果使用启动/依赖项,则可以隐藏。这是在门户中完成的,因此可能存在一些依赖性来缩短?
最佳答案
请尝试
Environment.GetEnvironmentVariable("appSettingName", EnvironmentVariableTarget.Process);
关于c# - 如何在 Portal C# 应用程序功能中访问 Azure 功能中的应用程序设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58087577/