简体   繁体   English

Azure函数环境变量

[英]Azure Function Environment Variables

I'm trying to create a super basic Azure Function, but am having trouble with environment variables. 我正在尝试创建一个超基本的Azure函数,但是遇到了环境变量的麻烦。 Following various tutorials online, 遵循各种在线教程,

        var config = new ConfigurationBuilder()
             .SetBasePath(context.FunctionAppDirectory)
             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
             .AddEnvironmentVariables()
             .Build();

        log.Info(config["AzureWebJobsStorage"]);

My local.settings.json looks like this: 我的local.settings.json看起来像这样:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "<language worker>",
    "AzureWebJobsStorage": "abc123",
    "AzureWebJobsDashboard": "abc123",
    "MyBindingConnection": "abc123"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "*"
  }
}

When I run this locally, that 当我在本地运行时,

log.Info(config["AzureWebJobsStorage"]); 

line returns nothing... but when I deploy to Azure I see "abc123" in the console. 行什么都没有返回...但是当我部署到Azure时,在控制台中看到“ abc123”。

If, however, I alter that line to 但是,如果我将该行更改为

log.Info(config["Values:AzureWebJobsStorage"]);

Then when I run locally, I see "abc123" but when I deploy to Azure I see nothing. 然后,当我在本地运行时,看到“ abc123”,但是当我部署到Azure时,什么也看不到。

Is there something I'm missing to be able to reach the environment variables the same way locally vs deployed? 我缺少能够以本地方式还是部署方式访问环境变量的东西吗?

EDIT: To clarify, these settings are configured in the app settings for the function: 编辑:为澄清起见,这些设置在该功能的应用程序设置配置的: 在此处输入图片说明

假设您正在使用〜2运行时作为Azure函数的目标,则可以通过以下方式访问配置值:

log.Info(Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process));

Those environmental variables work when you are testing your function locally. 在本地测试功能时,这些环境变量会起作用。 However, when you deploy to the Azure Function Portal, you need to setup your variables using their built-in system to handle Environmental Variables. 但是,当部署到Azure功能门户时,需要使用它们的内置系统来设置变量以处理环境变量。

Copy and past your key-values into the sections that I highlighted in the image below. 将键值复制并粘贴到下图中突出显示的部分中。

< < Azure Functions门户-环境

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 Azure Function 无法在 startup.cs 中正确加载我的环境变量? - Why would my Azure Function not load my environment variables correctly in startup.cs? 在 c# 中读取 azure 管道环境变量 - reading azure pipeline environment variables in c# 如何在Azure 2.5的IIS应用程序中设置环境变量并使用它们 - How to set environment variables and use them in IIS application in Azure 2.5 Azure门户中的Azure Function Environment.GetEnvironmentVariable对象引用错误 - Azure Function Environment.GetEnvironmentVariable object reference error in Azure portal Azure Function 中的可选变量由 BLOB 触发 - Optional variables in Azure Function triggered by BLOB 以编程方式为 .NET 5 Azure Function ServiceBusTrigger 连接字符串设置环境变量? - Set Environment Variable for .NET 5 Azure Function ServiceBusTrigger connection string programmatically? 是否可以从 Azure function 中保留环境设置? - Is it possible to persist an environment setting from within an Azure function? 如何在 ASP.NET 服务器的 web.config 文件中使用 Azure Pipeline 中设置的环境变量? - How to use environment variables set in Azure Pipeline in the web.config file of ASP.NET server? Azure DevOps 发布管道,从 C# 代码读取环境变量 - Azure DevOps Release Pipeline, Read Environment Variables from C# Code 在Azure Functions中读取环境变量时,为什么建议使用GetEnvironmentVariable而不是AppSettings - Why is GetEnvironmentVariable recommended to use over AppSettings when reading Environment variables in Azure Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM