简体   繁体   中英

Azure functions - how to insert array in configuration file

I'm using the local.settings.json file to store application settings for my Azure Function, as suggested here . I can access the Values of the application settings in the following example

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "AzureWebJobsDashboard": ""
  },
  "ConnectionStrings": {
    "SQLConnectionString": "myConnectionString"
  }
}

by using ConfigurationManager.ApplicationSettings["someValue"] or the connection string by using ConfigurationManager.ConnectionStrings["SQLConnectionString"] . However, when I try to insert an array as one of the Values:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "AzureWebJobsDashboard": "",
    "myArray": [
      {
        "key1": "value1",
        "key2": "value2"
      },
      {
        "key1": "value3",
        "key2": "value4"
      }
    ]
  },
  "ConnectionStrings": {
    "SQLConnectionString": "myConnectionString"
  }
}

I start getting exceptions (when I try to access the connection string, for instance). So my guess is that I'm not using the correct format for the array. Can arrays be used in the local.settings.json file? And if they can, what's the correct format?

According to the azure function source code Azure.Functions.Cli/Common/SecretsManager.cs , you could find it has a AppSettingsFile class, which used to read the setting from the local.settings.json file.

Some part of the AppSettingsFile class:

        public AppSettingsFile(string filePath)
        {
            _filePath = filePath;
            try
            {
                var content = FileSystemHelpers.ReadAllTextFromFile(_filePath);
                var appSettings = JsonConvert.DeserializeObject<AppSettingsFile>(content);
                IsEncrypted = appSettings.IsEncrypted;
                Values = appSettings.Values;
                ConnectionStrings = appSettings.ConnectionStrings;
                Host = appSettings.Host;
            }
            catch
            {
                Values = new Dictionary<string, string>();
                ConnectionStrings = new Dictionary<string, string>();
                IsEncrypted = true;
            }
        }

        public bool IsEncrypted { get; set; }
        public Dictionary<string, string> Values { get; set; } = new Dictionary<string, string>();
        public Dictionary<string, string> ConnectionStrings { get; set; } = new Dictionary<string, string>();

According to the code, it use JsonConvert.DeserializeObject method to convert the json file to appSettings object.

But the appSettings.Values property is directory type, it doesn't support array. So I don't suggest you use array as its setting.

I suggest you could try to convert the array as two string value. This will work well.

Like this:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "AzureWebJobsDashboard": "",
    "arrary1-key1": "value1",
    "arrary1-key2": "value2",
    "arrary2-key1": "value3",
    "arrary2-key2": "value4"
  },
  "ConnectionStrings": {
    "SQLConnectionString": "myConnectionString"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM