c# - 如何在 Portal C# 应用程序功能中访问 Azure 功能中的应用程序设置?

标签 c# azure configuration

尝试从门户中创建的 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/

相关文章:

javascript - 计时器触发Azure Function开始但未结束?

ubuntu - 在成功构建时通过 Jenkins 发送邮件?

c# - 按钮上的文本在更改 IsEnabled 时失去中心对齐

c# - 尝试为当前语言设置小数点分隔符,得到 "Instance is read Only"

c# - 在 VS 2010 的类设计器中可视化泛型继承的类型参数

azure - AKS 将 DNS 名称分配给私有(private) ClusterIP 地址

c# - 在运行时使用反射检查给定事件是否被订阅

azure - 从 A 系列升级到 D 系列 Azure 虚拟机

symfony - 如何在services.yml中引用parameters.yml?

azure - 如何根据解决方案配置更改azure函数local.settings.json?