Photo by Adi Goldstein

Use settings.json in your C# Azure Functions

Load local or production settings!

Posted by Damien Aicheh on 05/10/2022 · 6 mins

When you develop an Azure Function you will probably need to use some settings that will be defined by environment. Those settings files can be added to your source control only if they dont contains sensitive values, such as password, tokens or secrets.

In this tutorial we will see how to load settings.json file and override it with local values in your local.settings.json for development purpose.

Load the settings

To be able to use your settings files you need to load them when you start your Azure Function, which is in the Startup.cs file:

[assembly: FunctionsStartup(typeof(Company.Function.Startup))]
namespace Company.Function
{
    internal class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var configuration = BuildConfiguration(builder.GetContext().ApplicationRootPath);
            builder.Services.AddAppConfiguration(configuration);
        }

        private IConfiguration BuildConfiguration(string applicationRootPath)
        {
            var config =
                new ConfigurationBuilder()
                    .SetBasePath(applicationRootPath)
                    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile("settings.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables()
                    .Build();

            return config;
        }
    }
}

As you can see above, the BuildConfiguration method load two json files:

  • local.settings.json
  • settings.json

The local.settings.json file is where you can define the values for your project in your developer environment. This file must not be added to your source control because it’s specific of your own machine.

The settings.json file will be used in production. It is a good practice to have one settings.json.tmpl which will contain only the structure of the settings.json file so you can copy paste it and add the values you need in your local developer environment.

In fact, the files are loaded in the order where they are defined. Which means that in this example the local.settings.json will be override by the settings.json values if they are present. This technique is useful when you want to use local values on your machine and override them in production.

If you invert the two lines like this:

.AddJsonFile("settings.json", optional: true, reloadOnChange: true)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)

You will have the settings.json default values overrided by local.settings.json if you need. This can be use for instance, if you target a default url to your development environment online and you want to target another one in you local machine using localhost.

Use values defined in the files

For the purpose of this article, the json files will contain a definition or an ApiUrl:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "MyServerOptions": {
    "ApiUrl": "http://localhost"
  }
}

The Values object and IsEncrypted option that you see above are default parameters needed for your Azure Function to start correctly.

To consume the MyServerOptions section we will define a class:

public class MyServerOptions
{
    public string ApiUrl { get; init; }
}

Then we will create an extension method called AddAppConfiguration where we will add all configurations for our Azure Functions, below you can see how easy it is to load the MyServerOptions section:

internal static class ConfigurationServiceCollectionExtensions
{
    public static IServiceCollection AddAppConfiguration(this IServiceCollection services, IConfiguration config)
    {
        services.Configure<MyServerOptions>(config.GetSection(nameof(MyServerOptions)));
        return services;
    }
}

And then use it in the Configure method like we saw previously:

public override void Configure(IFunctionsHostBuilder builder)
{
    var configuration = BuildConfiguration(builder.GetContext().ApplicationRootPath);
    builder.Services.AddAppConfiguration(configuration);
}

Final touch

Now you know how to use settings files in your Azure Functions in production or in local environment.You will find full source code in this Github repository.

Happy coding !

You liked this tutorial ? Leave a star in the associated Github repository!

Do not hesitate to follow me on to not miss my next tutorial!