Azure functions appsettings.json instead of local.settings.json

 Currently one of the services I manage needs to properly set configurations similar to appsetting.json so that the parent name of the config doesnt need to change. This is because of service that calls for the config is looking specifically at the parent name. However since its a azure function using this I ran into an issue of using a different name rather than "Values" where db connections or other properties need to be in order to be read. I tried using the following example...

{

"CustomParent": { 

    "test":"true"

    }

   "Values": {

        "test": "true" 

    }

}

For my custom name I can grab the test value, just what's in values parent.

Solution

   var configuration = new ConfigurationBuilder()
             .SetBasePath(Directory.GetCurrentDirectory())
             .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
             .AddEnvironmentVariables()
             .Build();

The only bit of improvement I would make is creating a startup file to handle this...

[assembly: FunctionsStartup(typeof(Example.AzureAppFunctions.Startup))]
namespace Example.AzureAppFunctions
{
    public class Startup : FunctionsStartup
    {
        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            FunctionsHostBuilderContext context = builder.GetContext();

            builder.ConfigurationBuilder
                .AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: true, reloadOnChange: false)
                .AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false)
                .AddEnvironmentVariables();
        }

        public override void Configure(IFunctionsHostBuilder builder)
        {
            // Inject Settings as Options
            //builder.Services.AddOptions<string>()
            //                .Configure<IConfiguration>((settings, configuration) => { configuration.Bind("Configurations", settings); });
        }
    }
}

Popular posts from this blog

Installing Windows on Acer chromebook 15 cb3-532

Entity Framework: ToListAynsc & WhereAsync

NLog 4.6.2 Console Application