How to inject or use IConfiguration in Azure Function V3 with Dependency Injection when configuring a service

前端 未结 4 1890
挽巷
挽巷 2021-02-02 12:46

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

4条回答
  •  灰色年华
    2021-02-02 13:02

    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();
                    });
                }
            }
        }
    }
    

提交回复
热议问题