Skip to content

Configuration and FunctionsHostBuilderContext support

Brett Samblanet edited this page Sep 25, 2020 · 3 revisions

Note -- this is part of official documentation now at https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources

Version 1.1.0 of the Microsoft.Azure.Functions.Extensions package adds two new features to the FunctionsStartup class. This version of the package is currently in preview on nuget and supported in the following host versions:

  • Any Functions v2 host version greater than 2.0.14192.0
  • Any Functions v3 host version greater than 3.0.14191.0

Configuration support

There is a new override-able method on FunctionsStartup:

public virtual void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)

The IFunctionsConfigurationBuilder interface has one property:

IConfigurationBuilder ConfigurationBuilder { get; }

When overridden, the Azure Functions host will call into this method and provide an IConfigurationBuilder that can be used to register any ConfigurationProvider. This provider will then be used by the host to build the IConfiguration for use throughout the Job Host and your registered services.

Important

For Azure Function apps running in the Consumption or Premium plans, configuration values that are used in bindings cannot be modified with a custom configuration source. During startup, the host will check to see whether any properties have changed after calling into a FunctionsStartup class and if so the host will log an error and fail to start. This is because these plans require that the Functions Scale controller knows the proper settings to monitor for scale out, and if these values change, it can result in scaling errors.

FunctionHostBuilderContext support

FunctionsHostBuilderContext is a helpful class that can be accessed from either IFunctionsConfigurationBuilder or IFunctionsHostBuilder by calling the GetContext() extension method. For example:

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
    FunctionsHostBuilderContext context = builder.GetContext();
    ...
}

This class provides details that can be useful while registering configuration sources or services. This context is very similar to Microsoft.Extensions.Hosting.HostBuilderContext, but has some values that are specific to Azure Function applications.

Samples

See the DependencyInjection sample project for how override ConfigureAppConfiguration and use FunctionsHostBuidlerContext to register appsettings.json configuration sources: https://github.com/Azure/azure-functions-dotnet-extensions/blob/main/src/samples/DependencyInjection/Basic/SampleStartup.cs#L15-L24