1

I'm writing my first Azure function (based on .NET Core 2.x), and I'm struggling to extend the app settings with my custom mapping table.

I have a file local.settings.json, and I have tried to extend it with a "custom" config section that contains a few "key/value" pairs - something like this:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    ... (standard config settings) ...
  },
  ... (more default sections, like "Host" and "ConnectionStrings")
  "MappingTable" : {
    "01" : "Value1",
    "02" : "Value2",
    "03" : "Value3",
    "else" : "Value4"
  }
}

I get the IConfiguration injected into my worker class via constructor injection, and it works just fine for the "basic" default values stored in the "Values" section:

public MyWorker(IConfiguration config)
{
    _config = config;

    string runtime = _config["FUNCTIONS_WORKER_RUNTIME"];  // works just fine

    // this also works fine - the "customSection" object is filled
    var customSection = _config.GetSection("MappingTable");

    // but now this doesn't return any values
    var children = customSection.GetChildren();

    // and trying to access like this also only returns "null"
    var mapping = customSection["01"];
}

I'm stuck here - all the blog post and articles I find seem to indicate to do just this - but in my case, this just doesn't seem to work. What am I missing here? I'm quite familiar with the full .NET framework config system - but this here is new to me and doesn't really make a whole lot of sense just yet......

I have also tried to move the entire MappingTable section to appSettings.json - but that didn't change anything, I'm still getting back only null when trying to access my custom config section's values....

Thanks!

Update: all the suggested ways of doing this work just fine with a standard ASP.NET Core 2.1 web app - but in the Azure function, it's not working. Seems like code in an Azure Function is treating configuration differently than a regular .NET Core code base ...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

4

I did something similar using .net core 3.0

local.settings.json

{
  "AppSettings":{
    "MappingTable" : {
      "01" : "Value1"       
    }
  }
}

Reading appsettings:

private void AppSettings()
{
   var config = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                        .AddEnvironmentVariables()
                        .Build();

   var 01 = config["AppSettings:MappingTable:01"];
}

In the Azure portal, you need to add this as an application setting. In your Function App -> Configuration -Application Settings -> New Application setting enter image description here

Name:AppSettings:MappingTable:01
Value:Value1
sjokkogutten
  • 2,005
  • 2
  • 21
  • 24
  • Looked promising - but even so: I still only get a `null` back - no matter which setting I try.... I must be missing something small, but fundamental somewhere...... – marc_s Nov 14 '19 at 15:17
  • **Update:** this all works just fine with a standard ASP.NET Core 2.1 web app - but in the Azure function, it's not working. Seems like code in an Azure Function is treating configuration differently that regular .NET Core code ...... – marc_s Nov 14 '19 at 19:24
2

A R G H ! ! !

I knew it - such a stupid little mistake - but with rather massive consequences.....

In my startup class, I had this code (plus some more):

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddHttpClient();

        var config = new ConfigurationBuilder()
            .SetBasePath(Environment.CurrentDirectory)
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
        .....
    }
}    

but what was missing is this one line of code after the call to .Build() above:

        builder.Services.AddSingleton<IConfiguration>(config);

Somehow, the settings in the Values section of local.settings.json were present and accessible, but any custom config sections weren't.

Adding this one line has solved my problem - now I can easily read the MappingTable from my local.settings.json (or appsettings.json) file and use it in my code.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459