从 Azure 函数中的 local.settings.json 读取自定义设置 [英] Read custom settings from local.settings.json in Azure functions

查看:19
本文介绍了从 Azure 函数中的 local.settings.json 读取自定义设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 local.settings.json 文件中检索自定义设置.示例我正在尝试读取以下 local.settings.json 文件中存在的表列表

I am trying to retrieve a custom setting from local.settings.json file. Example I am trying to read tables list present in the below local.settings.json file

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "TableList": "TestTableName1,TestTableName2"
  }
}

使用下面的代码正在阅读它

Using the following code am reading it

string tableslist = ConfigurationManager.AppSettings["TableList"];

它可以工作,但我在一些地方读到这仅在本地调试时有效,在生产环境中部署后可能无法工作.有人能指出我如何以正确的方式做到这一点吗?还是问题只适用于连接字符串相关的设置?

and it works, but I've read in a few places that this works only when debugging locally, it might not work after it is deployed, in production environment. Could someone point me how to do this in the right way? Or the problem is only applicable to the connection string related settings?

推荐答案

@Kirk 和 @Slava 帮助您摆脱了困惑.只需添加一些细节供您参考.

@Kirk and @Slava have helped you get rid of confusion. Just add some details for you to refer.

默认情况下,发布既不会将 local.settings.json 上传到 Azure,也不会基于该本地文件对应用程序设置进行修改,因此我们需要在 Azure 门户上手动更新它们.我们也可以在 VS 发布面板上执行此操作.(如果我们需要在发布前更改设置,请先创建配置文件.)

By default, publication neither upload local.settings.json to Azure, nor makes modification on Application settings based on that local file, hence we need to update them manually on Azure portal. We can also do that on VS publish panel.(Create profile first if we need to change settings before publish.)

关于如何在应用设置中获取参数,需要注意的一点是 ConfigurationManager 在 v2 功能(运行时 beta)中不再支持,可能只会得到 null 或异常.对于 v1 函数(runtime ~1),它仍然有效.

About how to get parameters in app settings, one thing to note is that ConfigurationManager is no long supported in v2 function(runtime beta), may only get null or exception with it. For v1 function(runtime ~1), it still works.

  1. 适用于 v1 功能

  1. For v1 function

要读取 Azure 上的应用程序设置(以及 local.settings.json 中的 Values),建议使用 System.Environment.GetEnvironmentVariable($"{parameterName}").

To read Application settings on Azure(also Values in local.settings.json), System.Environment.GetEnvironmentVariable($"{parameterName}") is recommended.

转到连接字符串,不幸的是 GetEnvironmentVariable 仅适用于 Azure,因为连接字符串(ConnectionStrings in local.settings.json)未导入环境变量.所以我们需要 ConfigurationManager,它可以在 Azure 和本地环境中运行.当然它也可以读取应用程序设置.

Turn to Connection strings, unfortunately GetEnvironmentVariable only works on Azure because Connection strings(ConnectionStrings in local.settings.json) are not imported into Environment Variables. So we need ConfigurationManager, which works in both Azure and local env. Of course it can read Application settings as well.

对于 v2 功能,应用程序设置和连接字符串有两个选择.

For v2 function, two choices for both Application settings and Connection strings.

一种是使用GetEnvironmentVariable.我们可以参考这个列表 Azure 上连接字符串的前缀.

One is to use GetEnvironmentVariable. We can refer to this list for Prefixes of Connection String on Azure.

// Get Application settings
var appParameter= "AzureWebJobsStorage";
System.Environment.GetEnvironmentVariable($"{appParameter}");

// Get Connection strings(put local and Azure env together)
var connParameter= "MySqlAzureConnection";
var Prefix = "SQLAZURECONNSTR_";
var connectionString = System.Environment.GetEnvironmentVariable($"ConnectionStrings:{connParameter}");
if(string.IsNullOrEmpty(connectionString )){
   connectionString = System.Environment.GetEnvironmentVariable($"{Prefix}{connParameter}");
}

另一种是使用ConfigurationBuilder.添加ExecutionContext参数,用于定位函数应用目录.

Another one is to use ConfigurationBuilder. Add ExecutionContext parameter, which is used to locate function app directory.

[FunctionName("FunctionName")]
public static void Run(...,ExecutionContext context)
{
   //"Values" and "Connection" sections are injected into EnvironmentVariables automatically hence we don't need to load Json file again. 
   //Hence SetBasePath and AddJsonFile are only necessary if you have some custom settings(e.g. nested Json rather than key-value pairs) outside those two sections. It's recommended to put those setting to another file if we need to publish them.
   //Note that Function binding settings(e.g. Storage Connection String) must be EnvironmentVariables, i.e. must be stored in "Values" section.
    var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

    // Get Application Settings
    var appParameter= "AzureWebJobsStorage";
    string appsetting = config[$"{appParameter}"];

    // Get Connection strings
    var connParameter= "MySqlAzureConnection";
    string connectionString = config.GetConnectionString($"{connParameter}");
}

这篇关于从 Azure 函数中的 local.settings.json 读取自定义设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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