azure - 在 Azure 中使用 GetEnvironmentVariable 读取 local.settings.json 中的 ConnectionStrings

标签 azure environment-variables azure-functions

在 Azure 函数 v1 中,我尝试使用 GetEnvironmentVariable 静态方法读取 local.settings.json 中存在的连接字符串

这是我的local.settings.json 文件

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "ConnectionStrings": {
    "OnPremisesConnection": "Server=test;Initial Catalog=testdb;Integrated Security=SSPI;"
  }
}

使用以下代码读取连接字符串

string variableName = "OnPremisesConnection";
var res = Environment.GetEnvironmentVariable($"ConnectionStrings:{variableName}")

但结果是NULL。我在这里缺少什么?

最佳答案

好的。您可以尝试下一个:

var config = new ConfigurationBuilder()
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
 
 string connectionStr = config["ConnectionStrings:OnPremisesConnection"]

关于azure - 在 Azure 中使用 GetEnvironmentVariable 读取 local.settings.json 中的 ConnectionStrings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51786885/

相关文章:

azure - 为有问题的 azure databrick 添加 key 保管库范围

azure - 函数无法绑定(bind)到输出参数

azure - 将长时间运行的作业解耦为 azure 函数..如何获取整体状态?

c# - Activity 函数应返回 IEnumerable<IListBlobItem>

c# - .Net Core Azure认知搜索删除文档

azure - 如何一致且成功地将 Azure Service Fabric 应用程序部署到本地群集?

http - 我应该包括 Meteor 的 $ROOT_URL 环境变量的协议(protocol)吗?

javascript - 如何将 dockerfile 环境变量传递给node.js 文件?

java - 使用 bash 脚本设置 java ProcessBuilder 环境

Azure SignalR 服务实现 .NET 6 Azure Function 隔离进程