Cheranga
Published in

Cheranga

Azure Functions — Custom settings and Dependency Injection

Azure Functions — Serverless Awesomeness

Azure functions are awesome. As in many projects you’ll have to interact with different components which will need specific settings to be included. Also you might have the need to customize your application settings so that they’ll have different values targeting the respective environment deployed. This article explains how you can inject your custom application settings as strongly typed dependencies to your classes.

The code for this can be found in GitHub. Also I have created a nuget package called “Hatan.Azure.Functions.DependencyInjection.Extensions” if you would like to use it in your projects.

`local.settings.json` file.

The most popular way of using settings when developing Azure functions are to put them in the local.settings.json file. You can store the settings as name-value pairs and use it.

Custom application settings.

This file is used solely when developing locally and, by default this file is not pushed to your remote code repository. Also you cannot have any nested JSON structures because when you deploy the Azure function (through ARM templates or done manually) all your settings will have the same name-value pair structure. You can read more on this from here.

But, you can introduce custom JSON files to represent individual setting files and load them at the start-up of the function app. You can read about that approach in here.

Custom settings which we would like to populate.

Say for the purpose of demonstration below classes represent the application settings which we would like to populate and inject as dependencies to our classes.

Of course you can name these settings in anyway and use Environment.GetEnvironmentVariable to retrieve them from code.

But to load these settings using Microsoft’s DI and configuration frameworks they need to be separated with a “:” in local.settings.json file as shown below,

local.settings.json file with custom settings.

Registering the custom settings in the DI framework

Since the support of DI in Azure functions all you need to do is to install Microsoft.Azure.Functions.Extensions nuget package and add a class inheriting from the FunctionsStartup from the library. Then you can register your dependencies there.

Extension method to the rescue

As you can see above on line 10,11 that we use an extension method to load and register these custom settings in the DI framework.

There are two options provided to load and register.

  • Convention driven — in here the configuration section has the same name as the class which represents the settings.
  • More specific — in here the user specifies the configuration section name to be used when populating the settings.

Also note the interface ICustomApplicationSetting . It’s a marker interface so that any setting class to be used in this approach must implement it.

As shown above we use the default implementation of IConfiguration (Microsoft.Extensions.Configuration.ConfigurationRoot)to read the section from the settings file and bind it to the custom setting object which implements ICustomApplicationSetting .

But to inject as a strongly typed object itself not through IOptions<T> you need register it in the DI framework as shown below.

Then you can inject the custom setting as any other dependency.

Using this in a sample Azure Function application

Please refer the above mentioned GitHub link and you can use this sample Azure Function application which has been developed using these changes.

Conclusion

I have found this approach to be clean and concise especially when you are working in a project which have multiple different settings to be dealt with.

At a later article I’ll explain another approach which you can have custom JSON files as configuration files so that you’ll have some what like a modular approach rather than bloating all the settings in one file.

Thanks!

--

--

--

.NET, .NET Core, C#, ASP.NET Core, Architecture and Design

Recommended from Medium

Axure Tutorial: Make Line Chart with Repeater

Azure AD Risky Sign-ins Email Notification

Subquery AIRDROP

Flattening your Solidity files

Creating A Simple Spring Boot Application

9 Different CSS only animated loader with font awesome

8 Software Innovations That Will Change the World — Part 1

SyncDAO x IM community AMA RECAP

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cheranga Hatangala

Cheranga Hatangala

More from Medium

Azure AD B2C for .NET 4.8: Old Dogs Can Learn New Tricks

Using Azure Key Vault secrets in Azure DevOps Pipelines

Implementing Blue/Green Deployments with Azure Web Apps for Containers

Build Azure DevOps Pipelines for Dynamics 365 CRM