Search code examples
c#azureasp.net-coreazure-functionsasp.net-core-configuration

How to add custom configuration provider to azure functions


I implemented custom configuration provider and it works fine in a web app. But I also need to use it inside azure functions. And it seems like it's impossible at the moment. IFunctionsHostBuilder allows to get context which already have pre-built IConfigurationRoot instance. I cannot find a way to access to IConfigurationBuilder like in my web app. Has anyone managed to workaround it?


Solution

  • Thanks to Rogerson Nazário for the comments.

    It can be done by using a custom configuration provider in Azure Functions.

    By creating a class and making it to implement the IConfigurationProvider interface. And the created class is responsible for retrieving configuration values from the custom source and adding them to the IConfigurationBuilder.

    And also create a class that implements the IConfigurationSource interface. This class will be responsible for creating an instance of the custom configuration provider.

    In Startup class, add custom configuration source to the IConfigurationBuilder. You can do this by calling the Add method on the IConfigurationBuilder and passing in an instance of your IConfigurationSource implementation.

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        builder.ConfigurationBuilder
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Add<MyCustomConfigurationSource>();
    }
    

    Use the IConfiguration instance in Azure Function.

    You can do this by injecting an instance of IConfiguration into Azure Function using the FunctionName attribute.

    
    public static class MyFunction
    {
        [FunctionName("MyFunction")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log,
            IConfiguration configuration)
        {
            // Use configuration values here
        }
    }
    
    

    Using custom configuration providers to Azure Functions by overriding the ConfigureAppConfiguration method in function app's StartUp class.

    Thanks @ Glenn Gailey for the functions-dotnet-dependency-injection code.

    using System;
    using Microsoft.Azure.Functions.Extensions.DependencyInjection;
    using Microsoft.Extensions.Configuration;
    [assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
    namespace MyNamespace
    { public class Startup : FunctionsStartup { public 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(); }
    }
    }