Normally in a .NET Core project I would create a \'boostrap\' class to configure my service along with the DI registration commands. This is usually an extension method of
Here's an example that I was able to whip up; it establishes a connection to Azure App Configuration for centralized configuration and feature management. One should be able to use all DI features, such as IConfiguration
and IOptions
, just as they would in an ASP.NET Core controller.
NuGet Dependencies
Install-Package Microsoft.Azure.Functions.Extensions
Install-Package Microsoft.Extensions.Configuration.AzureAppConfiguration
Install-Package Microsoft.Extensions.Configuration.UserSecrets
Startup.cs
[assembly: FunctionsStartup(typeof(SomeApp.Startup))]
namespace SomeApp
{
public class Startup : FunctionsStartup
{
public IConfigurationRefresher ConfigurationRefresher { get; private set; }
public override void Configure(IFunctionsHostBuilder hostBuilder) {
if (ConfigurationRefresher is not null) {
hostBuilder.Services.AddSingleton(ConfigurationRefresher);
}
}
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder configurationBuilder) {
var hostBuilderContext = configurationBuilder.GetContext();
var isDevelopment = ("Development" == hostBuilderContext.EnvironmentName);
if (isDevelopment) {
configurationBuilder.ConfigurationBuilder
.AddJsonFile(Path.Combine(hostBuilderContext.ApplicationRootPath, $"appsettings.{hostBuilderContext.EnvironmentName}.json"), optional: true, reloadOnChange: false)
.AddUserSecrets(optional: true, reloadOnChange: false);
}
var configuration = configurationBuilder.ConfigurationBuilder.Build();
var applicationConfigurationEndpoint = configuration["APPLICATIONCONFIGURATION_ENDPOINT"];
if (!string.IsNullOrEmpty(applicationConfigurationEndpoint)) {
configurationBuilder.ConfigurationBuilder.AddAzureAppConfiguration(appConfigOptions => {
var azureCredential = new DefaultAzureCredential(includeInteractiveCredentials: false);
appConfigOptions
.Connect(new Uri(applicationConfigurationEndpoint), azureCredential)
.ConfigureKeyVault(keyVaultOptions => {
keyVaultOptions.SetCredential(azureCredential);
})
.ConfigureRefresh(refreshOptions => {
refreshOptions.Register(key: "Application:ConfigurationVersion", label: LabelFilter.Null, refreshAll: true);
refreshOptions.SetCacheExpiration(TimeSpan.FromMinutes(3));
});
ConfigurationRefresher = appConfigOptions.GetRefresher();
});
}
}
}
}