Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an appsettings.json file to my Azure Function 3.0 configuration?

The new Azure Function 3.0 SDK provides a way to implement a Startup class. It gives access to the collection of services that are available by dependency injection, where I can add my own components and third-party services.

But I don't know how to use a configuration file.

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
...

My third party services take large structures as parameter, and those configuration files are copied with binaries. I can copy them in a subsection of an appsettings.json file:

{
  "MachineLearningConfig" : {
     ( about 50+ parameters and subsections )
  }
}

Configuration values are updated according to the environment of deployment . I use Azure Devops's File Transform Task for that: production values are different from staging and dev values.

Given the documentation https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection the way to load those options is:

builder.Services.AddOptions<MachineLearningConfig>()
                .Configure<IConfiguration>((settings, configuration) =>
                                           {
                                                configuration.GetSection("MachineLearningConfig").Bind(settings);
                                           });

But that requires to add all settings as key/value strings in the host's environment, and that is what I do not want to do. There are too many of them and that is not as easy to maintain as in a json configuration file.

I copied that appsettings.json alongside the host.json.

But the appsettings.json file read at startup by the Azure Function SDK is not my application's appsettings.json but Azure Function tools's appsettings.json. So configuration.GetSection("MachineLearningConfig") returns empty values as there is no appsettings.json file in the Azure Function tools bin folder.

So, my question: how to have my MachineLearningConfig section read from my appsetting.json file injected as IOption<MachineLearningConfig> in my app ?

like image 716
Anthony Brenelière Avatar asked Jan 29 '20 01:01

Anthony Brenelière


People also ask

How do I add Appsettings to json?

Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.

Where is Appsettings json located?

appsettings. json is one of the several ways, in which we can provide the configuration values to ASP.NET core application. You will find this file in the root folder of our project. We can also create environment-specific files like appsettings.

What is host json in Azure function?

The host. json metadata file contains configuration options that affect all functions in a function app instance. This article lists the settings that are available starting with version 2. x of the Azure Functions runtime.


1 Answers

In the startup class:

    IConfigurationRoot config = new ConfigurationBuilder()
              .SetBasePath(Environment.CurrentDirectory)
              .AddJsonFile("someSettings.json", optional: true, reloadOnChange: true)
              .AddEnvironmentVariables()
              .Build();

Add a json file to you project that holds the settings. Note that local.settings.json is ignored/removed during deployment. (Name the file something else.)

like image 59
Zsolt Bendes Avatar answered Sep 28 '22 15:09

Zsolt Bendes