How to inject appsettings.<Environment>.json in Azure Functions

Tharaka Madhusanka
4 min readJan 3, 2021

Hello GeeK FolkS :D First, I’d like to wish you for dawned 2021 New Year, to be a greatest year ahead, but don’t forget MASK :D. Anyway, let’s jump in to the topic, ‘How to inject appsettings.<Environment>.json in Azure Functions’. By the title you may be able to understand what is the thing, I am going to discuss here. Let’s read then,

Prerequisites

  • Should have understanding on Azure Functions
  • Should have understanding on .NET Core Dependency Injection in Azure Functions
  • Deployment and Configuration of Azure Function Apps

Environment

  • .NET Core 3.1

Problem

When we are working on Enterprise level application, we may have to work on different environments, such as Local, Development, QA, UAT/Staging, Production.

In each environment may have different set of configurations, consumed by the application. As an example, QA deployed application may use separate QA Database, Production Application may use separate Production Database.

Such a way we may need to switch configurations on different environments, for the same application. That’s where the appsettings.<Environment>.json is introduced.

This is so familiar when we are working with ASP.NET Applications. (I’m talking only Microsoft here :P ). But when it comes to Azure Functions it is completely different.

By default it is not coming to support this appsettings.<Environment>.json. We can create environment variables in Azure Function Apps in each environment, to keep such different configurations. But we do have to do this manually or using ARM (Azure Resource Manager) Templates, and inside the Function we can read those configurations, using ConfigurationManager.

Instead of using Configuration Variables/Environment Variables and ConfigurationManager, I wanted to use appsettings.<Environment>.json. The benefits what I see on this are,

  1. No need to put extra effort to create Environment/Configuration Variables in each Azure Function App.
  2. Can maintain configurations in a clean, readable and separate manner
  3. One time Injection and can be read each configuration using IOptions<T>.
  4. Less probability to missing configurations as we maintain all are at the same place.
Uh-La-La-La

Solution

Step 1: Create different appsettings.<Environment>.json files based on your requirement. For this example I’ve created 3 appsettings.<Environment>.json files

Different appsettings files

Set property ‘Copy to Output Directory’ to ‘Copy if newer’.

Configure Copy to Output Directory

Step 2: Next add the following code snippets in your Startup.cs. This code snippet overrides the method ConfigureAppConfiguration.

// Override ConfigureAppConfiguration method to inject appsettings json filespublic override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder){FunctionsHostBuilderContext context = builder.GetContext();builder.ConfigurationBuilder.AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: true, reloadOnChange: false).AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false).AddEnvironmentVariables();}

Set your files path accordingly. I used root, as I keep files in the root directory.

Step 3: Add the following code snippet in your Startup.cs to inject specific section in appsettings.json to DTO class.

public override void Configure(IFunctionsHostBuilder builder){// Inject Settings as Optionsbuilder.Services.AddOptions<AppSettingsConfigurationOptions>().Configure<IConfiguration>((settings, configuration) => { configuration.Bind("Configurations", settings); });}

Step 4: Now we are almost done. But last we need to say what is the runtime environment of our Azure Function. For that we need to set ‘AZURE_FUNCTIONS_ENVIRONMENT’.

By Default, Azure set AZURE_FUNCTIONS_ENVIRONMENT as Development when you are running the function locally(in your PC) and set AZURE_FUNCTIONS_ENVIRONMENT as Production when you are running the application in Azure App Service (In Azure Portal).

Set in Locally
Set in Azure

That’s it :D Now we have set up all, Just Run & Check ;) For the demonstration purpose I created Azure HTTP Trigger Function which returns the Environment Name in the appsettings.json.

Environment name Development

Find the sample application here, https://github.com/TharakaMadhusanka/azure-function-appsettings-sample

References

  1. https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources
  2. https://docs.microsoft.com/en-us/azure/azure-functions/functions-app-settings

So that’s all. Read, if you enjoy, then Clap, Share., else you have something to say, put a Comment :D

Happy Coding ! Happy Masking !! My Dancing Code ;)

--

--