Customizing configuration sources for Azure Functions
Jaco Jansen van Vuuren
Software DeveloperEver needed to add a YAML/JSON file as a configuration source for your Azure Function app? Up until this point you would have been stuck using some workaround - but the functions team just rolled out preview support for customizing configuration sources.
To get started you'll need to install the preview release of Microsoft.Azure.Functions.Extensions
from NuGet (make sure you tick "Include prerelease").
After you've installed the preview package you simply need to override ConfigureAppConfiguration
in your Startup
class.
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
[assembly: FunctionsStartup(typeof(JacoJvv.Demo.Function.Startup))]
namespace JacoJvv.Demo.Function
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddOptions<HostOptions>()
.Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection("apiHosts").Bind(settings);
});
}
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
var context = builder.GetContext();
builder.ConfigurationBuilder
.AddYamlFile(Path.Combine(context.ApplicationRootPath, "hostConfiguration.yml"), optional: true, reloadOnChange: false)
.AddEnvironmentVariables();
}
}
}
That's it. Simple. A lot simpler than this.
You can read more about it on the official Microsoft docs if you are interested.