We need to add configuration providers to the native IConfiguration that is supplied to the Azure Functions natively. Currently we are completely replacing it with our custom Iconfiguration using the following code:
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
...
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddAzureKeyVault(...)
.AddJsonFile("local.settings.json", true, true)
.AddEnvironmentVariables()
.Build();
builder.Services.AddSingleton<IConfiguration>(configuration);
builder.Services.AddSingleton<IMyService, MyService>();
}
}
Some context
MyService needs in its constructor values from the KeyVault provider and also other instances like Application Insights, etc. If we leave the default IConfiguration, it doesn't have the KeyVault values. If we create the MyService instance with a factory, we need to manually provide the App Insights instance, etc. Currently replacing the IConfiguration compiles and the function runs. But it breaks other default behavior like not taking the configurations from the host.json (we are trying to configure the queue trigger). Using the default IConfiguration correctly reads the settings from host.json.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…