存储 Azure Function 环境变量的最佳位置

Best place to store environment variables for Azure Function(存储 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

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 环境变量的最佳位置

基础教程推荐