Best place to store environment variables for Azure Function(存储 Azure Function 环境变量的最佳位置)
问题描述
我正在使用几个 api 键在本地测试一个 azure 函数.存储环境变量的最佳位置是什么以及如何访问它们?我试过了
I'm testing an azure function locally with several api keys. Whats the best place to store environment variables and how do I access them? I tried
System.Environment.GetEnvironmentVariable("name")}
但我不确定环境变量的存储位置.
but I'm not sure where the environment variable is stored.
谢谢!
推荐答案
你应该有一个名为 local.settings.json 的文件.这是 Functions-Run-Local的 azure 网站一个>
You should have a file called local.settings.json. Here is the azure website for Functions-Run-Local
说明
这些设置也可以在您的代码中作为环境变量读取.在 C# 中,使用 System.Environment.GetEnvironmentVariable 或 ConfigurationManager.AppSettings.在 JavaScript 中,使用 process.env.指定为系统环境变量的设置优先于 local.settings.json 文件中的值.
These settings can also be read in your code as environment variables. In C#, use System.Environment.GetEnvironmentVariable or ConfigurationManager.AppSettings. In JavaScript, use process.env. Settings specified as a system environment variable take precedence over values in the local.settings.json file.
local.settings.json 示例
example local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<connection string>",
"AzureWebJobsDashboard": "<connection string>"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*"
},
"ConnectionStrings": {
"SQLConnectionString": "Value"
}
}
它说您需要将应用程序设置放在 local.settings.json 中的 Values 属性下.
It says that you need to put the application settings under the Values property within the local.settings.json.
为了检索,我使用了 ConfigurationManager.AppSettings["CustomSetting"]
,因为它可以让您检索连接字符串.
To retrieve I used ConfigurationManager.AppSettings["CustomSetting"]
as it lets you retrieve connection strings.
我一直在玩这个,发现你必须有一个字符串键和一个字符串值.当我尝试创建一个小节时出现错误(就像您在 appsettings.json 中所做的那样).我必须让 local.settings.json 看起来像这样:
I have just been playing around with this and discovered that you have to have a a string key and a string value. I got an error when I tried having a subsection (like you would in an appsettings.json). I had to have the local.settings.json look like this:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<connection string>",
"AzureWebJobsDashboard": "<connection string>"
"CustomSetting":"20"
}
}
这篇关于存储 Azure Function 环境变量的最佳位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:存储 Azure Function 环境变量的最佳位置
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01