存储Azure函数的环境变量的最佳位置 [英] Best place to store environment variables for Azure Function

查看:61
本文介绍了存储Azure函数的环境变量的最佳位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用几个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函数的环境变量的最佳位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆