Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex object app settings in Azure Function

I have these entries in my local.settings.json

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "whateverstorageaccountconnectionstring",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    },
    "BusinessUnitMapping": {
        "Values": {
            "Connections": "CON",
            "Products": "PRD",
            "Credit & Affordability": "CAA",
            "Accounts Receivable": "ARC",
            "Identity":  "IDT"
        }
    }
}

I have this code to read the values in startup

services.Configure<BusinessUnitMapping>(options => configuration.GetSection("BusinessUnitMapping").Bind(options));

where BusinessUnitMapping is

public class BusinessUnitMapping
{
  public Dictionary<string, string> Values { get; set; }
  public BusinessUnitMapping()
  {
      Values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  }
}

when the I run the function app locally, it can read these settings into BusinessUnitMapping without any issue.

Advanced Edit for Application Settings in Azure Portal only allows simple Key Value pair as below

[
  {
    "name": "AzureWebJobsDashboard",
    "value": "DefaultEndpointsProtocol=Somevalue",
    "slotSetting": false
  },
  {
    "name": "AzureWebJobsStorage",
    "value": "DefaultEndpointsProtocol=Somevalue",
    "slotSetting": false
  },
  ...
]

The questions

  1. Is this a correct approach to store the complex app settings in Azure Function?
  2. How do I get BusinessUnitMapping configured in Azure Portal for the Function App that I have deployed?

-Alan-

like image 639
Alan B Avatar asked Sep 05 '19 01:09

Alan B


People also ask

How do I change my runtime settings?

Under Settings, choose Configuration. In the Function runtime settings tab, locate the Runtime version. Note the specific runtime version. In the example below, the version is set to ~4 .

What is AzureWebJobsStorage used for?

AzureWebJobsStorage. The Azure Functions runtime uses this storage account connection string for normal operation. Some uses of this storage account include key management, timer trigger management, and Event Hubs checkpoints. The storage account must be a general-purpose one that supports blobs, queues, and tables.


1 Answers

  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

like image 187
Matt Avatar answered Sep 22 '22 05:09

Matt