skip to Main Content

Language Stack: .NET 6 LTS

IDE: Visual Studio 2022 Community

local.settings.json:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
      "FUNCTIONS_WORKER_RUNTIME": "dotnet",
      "toAddress": [
        {
          "mail1": "[email protected]",
          "mail2": "[email protected]",
          "mail3": "[email protected]"
        }
      ]
    }
}

Function Class:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Configuration;
using Microsoft.Extensions.Configuration;

namespace ArrayVarPoC
{
    public class Function1
    {

        private readonly IConfiguration _config;
        public Function1(IConfiguration config)
        {

            _config = config;

        }
        [FunctionName("Function1")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string[] toAddress = _config.GetValue<string[]>("toAddress");
            log.LogInformation($"[{toAddress.ToString}]");
            

            string responseMessage = "Hello Fish, This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

I’m able to get the single value of the variable defined in local.settings.json but not the array variable values.

I tried to get the array variable values defined the local.settings.json file to the function class but not sure how to write the code for it.

Could anyone help me how to get this?

2

Answers


  1. I think you need to write in an array format to get the values.

    "Arrayofitems": {
    "conn1": "testconnectionString1",
    "conn2": "testconnectionString2",
    "toAddresses": [
      "Address1",
      "Address2",
      "Address3"
    ]
    

    }

    var section = builder.Configuration.GetSection($"Arrayofitems:toAddresses");
    string[] values = section.Get<string[]>();
    

    you will get the array values on the "values" variable.

    enter image description here

    Login or Signup to reply.
  2. I have tried to retrieve the Values with your given Configuration, even I got null values.

    enter image description here

    • Tried in multiple ways with GetSection and GetValue, but unable to retrieve the values in Array format.

    • AFAIK, we can retrieve the values from configuration when the Key-Values are in String format.

    Thanks @MuthuKumaranMurugaachari-MSFT for the explanation.
    In addition to the answer from MS Q&A, check the below code which worked for me.

    • Change the Configurations in the local.settings.json file as below.

    My local.settings.json file:

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "mail1": "[email protected]",
        "mail2": "[email protected]",
        "mail3": "[email protected]" 
      } 
    }
    

    My Function1.cs file:

     [FunctionName("ConfigValues")]
            public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,ILogger log)
            {
                log.LogInformation("C# HTTP trigger function processed a request.");     
                var myconfig = new
                {
                    mail1 = Environment.GetEnvironmentVariable("mail1"),
                    mail2 = Environment.GetEnvironmentVariable("mail2"),
                    mail3 = Environment.GetEnvironmentVariable("mail3"),
                };
               
                return new OkObjectResult(myconfig);
              
            }
    

    Output:

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search