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
I think you need to write in an array format to get the values.
}
you will get the array values on the "values" variable.
I have tried to retrieve the Values with your given Configuration, even I got null values.
Tried in multiple ways with
GetSection
andGetValue
, 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.
local.settings.json
file as below.My
local.settings.json
file:My
Function1.cs
file:Output: