存储 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 环境变量的最佳位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)