如何在Azure函数的AppSettings.json中添加配置值。有什么结构吗?

Har*_*wal 11 appsettings azure azure-functions

向其添加密钥的标准结构是什么appsettings.json?另外,如何读取我们的那些值run.csx?通常在app.config中,我们ConfigurationManager.GetSettings["SettingName"]; 在Azure Function中是否有任何类似的实现?

Stu*_*tLC 12

在Azure Functions 2.x中,您需要使用包中包含的.Net核心配置管理样式Microsoft.Extensions.Configuration。这使您可以settings.json在开发计算机上创建本地文件,以便在json文件的ValuesConnectionString部分中进行本地配置。在local json设置文件未发布天青,相反,天青将获得与功能相关联的应用程序设置的设置。

在功能代码中,接受type类型的参数,Microsoft.Azure.WebJobs.ExecutionContext context然后可以在其中构建IConfigurationRoot提供程序:

[FunctionName("MyFunction")]
public static async Task Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer,
    TraceWriter log, Microsoft.Azure.WebJobs.ExecutionContext context, 
    CancellationToken ctx)
{
   var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

    // This abstracts away the .json and app settings duality
    var myValue = config["MyKey"];

    var myConnString = config.GetConnectionString("connString");
    ... etc
Run Code Online (Sandbox Code Playgroud)

AddJsonFile允许你添加一个地方发展配置文件如local.settings.json包含本地开发值(不公开)

{
  "IsEncrypted": false,
  "Values": {
    "MyKey": "MyValue",
     ...
   },
   "ConnectionStrings": {
      "connString": "...."
}
Run Code Online (Sandbox Code Playgroud)

尽管似乎不鼓励将ConnectionStrings用于除EF之外的任何其他功能

部署到Azure后,您可以在功能应用程序设置刀片上更改设置的值:

应用配置


Kev*_*ith 7

如前所述这里

这些设置也可以在您的代码中作为环境变量读取。在C#中,使用System.Environment.GetEnvironmentVariableConfigurationManager.AppSettings。在JavaScript中,使用process.env。指定为系统环境变量的设置优先于local.settings.json文件中的值。