gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:20:35 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com