在 VS 2017 Azure Function 开发中从 local.settings.json 读取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46714677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:01:51  来源:igfitidea点击:

Read values from local.settings.json in VS 2017 Azure Function development

jsonazureazure-functionsappsettings

提问by Venky

I am writing an Azure function in VS 2017. I need to set up a few custom configuration parameters. I added them in local.settings.jsonunder Values.

我正在 VS 2017 中编写 Azure 函数。我需要设置一些自定义配置参数。我local.settings.json在下添加了它们Values

{
   "IsEncrypted":false,
   "Values" : {
      "CustomUrl" : "www.google.com",
       "Keys": { 
           "Value1":"1",
           "Value2" :"2"
       }
   }
}

Now, ConfigurationManager.AppSettings["CustomUrl"]returns null.

现在,ConfigurationManager.AppSettings["CustomUrl"]返回 null。

I'm using:

我正在使用:

  • .NET Framework 4.7
  • Microsoft.NET.Sdk.Functions 1.0.5
  • System.Configuration.ConfigurationManager 4.4.0
  • Azure.Functions.Cli 1.0.4
  • .NET 框架 4.7
  • Microsoft.NET.Sdk.Functions 1.0.5
  • 系统.配置.配置管理器 4.4.0
  • Azure.Functions.Cli 1.0.4

Am I missing something?

我错过了什么吗?

采纳答案by Fei Han

Firstly, I create a sample and do a test with your local.settings.jsondata, as Mikhail and ahmelsayed said, it works fine.

首先,我创建了一个示例并使用您的local.settings.json数据进行测试,正如 Mikhail 和 ahmelsayed 所说,它工作正常。

Besides, as far as I know, Valuescollection is expected to be a Dictionary, if it contains any non-string values, it can cause Azure function can not read values from local.settings.json.

此外,据我所知,Valuescollection 应该是一个 Dictionary,如果它包含任何非字符串值,它会导致 Azure 函数无法从 local.settings.json 读取值。

My Test:

我的测试:

ConfigurationManager.AppSettings["CustomUrl"]returns null with the following local.settings.json.

ConfigurationManager.AppSettings["CustomUrl"]使用以下 local.settings.json 返回 null。

{
  "IsEncrypted": false,
  "Values": {
    "CustomUrl": "www.google.com",
    "testkey": {
      "name": "kname1",
      "value": "kval1"
    }
  }
}

enter image description here

在此处输入图片说明

回答by jayasurya_j

Environment.GetEnvironmentVariable("key")

Environment.GetEnvironmentVariable("key")

I was able to read values from local.settings.json using the above line of code.

我能够使用上面的代码行从 local.settings.json 读取值。

回答by Ashish-BeJovial

If you are using TimeTrigger based Azure function than you can access your key (created in local.settings.json) from Azure Function as below.

如果您使用基于 TimeTrigger 的 Azure 函数,则可以从 Azure 函数访问您的密钥(在 local.settings.json 中创建),如下所示。

[FunctionName("BackupTableStorageFunction")]
public static void Run([TimerTrigger("%BackUpTableStorageTriggerTime%")]TimerInfo myTimer, TraceWriter log, CancellationToken cancellationToken)

回答by Nivedha Lakshmanan

Azure function copies the binaries to the bin folder and runs using the azure function cli, so it searches for the local.settings.json, so make sure you have set the "Copy to Output Directory"to "Copy Always"

您已设置Azure的功能使用复制湛蓝的功能CLI,那么它将搜索local.settings.json,所以一定要确保二进制文件bin文件夹并运行"Copy to Output Directory""Copy Always"

enter image description here

在此处输入图片说明

回答by Herberth Gomez

Hey you mmight be able to read the properties while debugging, but once you go and try to deploy that in azure, those properties are not going to work anymore. Azure functions does not allow nested properties, you must use all of them inline in the "Values" option or in "ConnectionStrings". Look at this documentation as reference: https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings

嘿,您可能能够在调试时读取属性,但是一旦您尝试在 azure 中部署它,这些属性将不再起作用。Azure 函数不允许嵌套属性,您必须在“值”选项或“ConnectionStrings”中内联使用所有这些属性。查看此文档作为参考:https: //docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings