Complex object app settings in Azure Function Complex object app settings in Azure Function azure azure

Complex object app settings in Azure Function


  1. Is this a correct approach to store the complex app settings in Azure Function?

This is still an open question: see this github issue asking exactly this

  1. How do I get BusinessUnitMapping configured in Azure Portal for the Function App that I have deployed?

My current preferred approach is to use the options pattern with a delegate that uses GetEnvironmentVariable which will work both locally and in Azure. The downside is that you can't create complex types in the local settings file itself, but your object can be as complex as you like.

A simple example:

In local.settings.json:

{  ...  "Values": {    "AzureWebJobsStorage": "UseDevelopmentStorage=true",    ...    "SomeSection:Setting1": "abc",    "SomeSection:Setting2": "xyz",  },  ...}

In your startup:

services.Configure<MySettingsPoco>(o =>{    o.Setting1 = Environment.GetEnvironmentVariable("SomeSection:Setting1");    o.Setting2 = Environment.GetEnvironmentVariable("SomeSection:Setting2");});

Then in Azure you can create these settings as follows:

enter image description here


1. Is this a correct approach to store the complex app settings in Azure Function?

We can save these entries in the app settings. For more details, please refer to the document. The detailed steps are as below.

Set app settings on Azure Portalenter image description hereenter image description here

2. How do I get BusinessUnitMapping configured in Azure Portal for the Function App that I have deployed?

According to my test, we can use the following code to do that

#r "Newtonsoft.Json"using System.Net;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Primitives;using Newtonsoft.Json;using System.Collections.Generic;public static async Task<IActionResult> Run(HttpRequest req, ILogger log){    log.LogInformation("C# HTTP trigger function processed a request.");    var test =Environment.GetEnvironmentVariable("test",EnvironmentVariableTarget.Process);    log.LogInformation(test);    var values= JsonConvert.DeserializeObject<Dictionary<string, string>>(test);    var businessUnitMapping = new BusinessUnitMapping();    businessUnitMapping.Values = values;     log.LogInformation(businessUnitMapping.Values["Products"]);    string name = req.Query["name"];    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();    dynamic data = JsonConvert.DeserializeObject(requestBody);    name = name ?? data?.name;    return name != null        ? (ActionResult)new OkObjectResult($"Hello, {name}")        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");}public class BusinessUnitMapping    {        public Dictionary<string, string> Values { get; set; }        public BusinessUnitMapping()        {        }    }

enter image description here