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

查看:454
本文介绍了从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"
  }
}

使用下面的代码来阅读

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.)

关于如何在应用程序设置中获取参数,需要注意的一件事是v2函数(运行时beta)不再支持ConfigurationManager,它可能只会获得null或异常.对于v1函数(运行时〜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上有效,因为连接字符串(local.settings.json中的ConnectionStrings)没有导入到环境变量中.因此,我们需要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天全站免登陆