How to add an appsettings.json file to my Azure Function 3.0 configuration?

后端 未结 6 627
轻奢々
轻奢々 2021-01-18 02:52

The new Azure Function 3.0 SDK provides a way to implement a Startup class. It gives access to the collection of services that are available by dependency injection, where I

6条回答
  •  野的像风
    2021-01-18 03:27

    After some research, I came across this thread on Githib

    using appsettings.json + IConfiguration in Function App

    From which I crafted the following extension based on the comments and suggestions that showed to have worked.

    public static class FunctionHostBuilderExtensions {
        /// 
        /// Set up the configuration for the builder itself. This replaces the 
        /// currently registered configuration with additional custom configuration.
        /// This can be called multiple times and the results will be additive.
        /// 
        public static IFunctionsHostBuilder ConfigureHostConfiguration (
            this IFunctionsHostBuilder builder, 
            Action configureDelegate) {
            IServiceCollection services = builder.Services;            
            var providers = new List();            
            //Cache all current configuration provider
            foreach (var descriptor in services.Where(d => d.ServiceType == typeof(IConfiguration)).ToList()) {
                var existingConfiguration = descriptor.ImplementationInstance as IConfigurationRoot;
                if (existingConfiguration is null) {
                    continue;
                }
                providers.AddRange(existingConfiguration.Providers);
                services.Remove(descriptor);
            }
            //add new configuration based on original and newly added configuration
            services.AddSingleton(sp => {
                var configurationBuilder = new ConfigurationBuilder();                    
                //call custom configuration
                configureDelegate?.Invoke(sp, configurationBuilder);                
                providers.AddRange(configurationBuilder.Build().Providers);                
                return new ConfigurationRoot(providers);
            });            
            return builder;
        }
    }
    

    The main idea is to extract all the currently registered configuration related types, create a new builder, apply custom configuration and build a new configuration with the original and custom configuration details merged into one.

    It would then be used in Startup

    public class Startup : FunctionsStartup {
        public override void Configure(IFunctionsHostBuilder builder) {
            builder.ConfigureHostConfiguration((sp, config) => {
                var executioncontextoptions = sp.GetService>().Value;
                var currentDirectory = executioncontextoptions.AppDirectory;
    
                config
                    .SetBasePath(currentDirectory)
                    .AddJsonFile("appSettings.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables();
    
                //if there are multiple settings files, consider extracting the list,
                //enumerating it and adding them to the configuration builder.
            });
    
            builder.Services
                .AddOptions()
                .Configure((settings, configuration) => {
                    configuration.GetSection("MachineLearningConfig").Bind(settings);
                });
        }
    }
    

    The above should now be able to get the settings from your custom configuration.

提交回复
热议问题