How to modify IConfiguration natively injected in Azure Functions

后端 未结 3 1722
余生分开走
余生分开走 2020-12-20 22:45

We need to add configuration providers to the native IConfiguration that is supplied to the Azure Functions natively. Currently we are completely re

3条回答
  •  死守一世寂寞
    2020-12-20 23:08

    A simpler solution would be to override the ConfigureAppConfiguration method in your FunctionStartup class (https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources).

    The following example takes the one provided in the documentation a step further by adding user secrets.

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        FunctionsHostBuilderContext context = builder.GetContext();
    
        builder.ConfigurationBuilder
            .SetBasePath(context.ApplicationRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
            .AddJsonFile($"appsettings.{context.EnvironmentName}.json", optional: true, reloadOnChange: false)
            .AddUserSecrets(Assembly.GetExecutingAssembly(), true, true)
            .AddEnvironmentVariables();
    }
    

    By default, configuration files such as appsettings.json are not automatically copied to the Azure Function output folder. Be sure to review the documentation (https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources) for modifications to your .csproj file. Also note that due to the way the method appends the existing providers it is necessary to always end with .AddEnvironmentVariables().

    A deeper discussion on configuration in an Azure Function can be found at Using ConfigurationBuilder in FunctionsStartup

提交回复
热议问题