0

I am currently migrating a dotnet-isolated v3 function (.net 5) to a non-isolated v4 azure function (.net 6)

in v3 dotnet-isolated I had a console application and so a Program.cs which contained the following code:

public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureAppConfiguration(AddAzureAppConfig)
                .Build();

            host.Run();
        }

     private static void AddAzureAppConfig(IConfigurationBuilder builder)
        {
            var azureAppConfigurationEndpoint = Environment.GetEnvironmentVariable("AzureAppConfiguration");

            builder.AddAzureAppConfiguration(options =>
                  options.Connect(azureAppConfigurationEndpoint).ConfigureKeyVault(kv =>
                  {
                      kv.SetCredential(new DefaultAzureCredential());
                  }).Select(KeyFilter.Any, LabelFilter.Null)
            );
        }

Now in v4 I have a library instead and so no program.cs. Instead I created a Startup.cs derived from FunctionsStartup

 public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();
...
}
}

As far as I understood from this StackOverFlow-Question v4 Azure Functions don't have any IConfigurationBuilder and don't need to if you directly want to access any configuration values.

Now my issue is that while AddAzureAppConfiguration inside the Microsoft.Extension.Configuration.AzureAppConfigurationExtensions has a overload I could use (AddAzureAppConfiguration(this IServiceCollection services)) but it is missing the Action<AzureAppConfigurationOptions> action parameter (which the IConfigurationBuilder- variant had) which I need to add the KeyVault options.

What do I have to modify to use the same approach I was using with my AddAzureAppConfig() that worked on the v3 isolated console application approach?

Ole Albers
  • 8,175
  • 8
  • 65
  • 145
  • Have you tried not using '.ConfigureKeyVault()' at all and just configure everything in Azure? I know that's not your question but maybe something that might help. Here's also some more information (https://stackoverflow.com/questions/62960764/how-to-modify-iconfiguration-natively-injected-in-azure-functions/63124002#63124002) – lopezbertoni Mar 10 '22 at 17:02

1 Answers1

1

You can override the ConfigureAppConfiguration method in the FunctionsStartup. Inside the method, you can call ConfigurationBuilder.AddAzureKeyVault which is very similar to function v3.

Example below:

    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {


        }

        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            var builtConfig = builder.ConfigurationBuilder.Build();
            var keyVaultEndpoint = builtConfig["AzureKeyVaultEndpoint"];

            if (!string.IsNullOrEmpty(keyVaultEndpoint))
            {

                var config = builder.ConfigurationBuilder
                        .AddAzureKeyVault(keyVaultEndpoint)
                    .Build();

                var kvSecret = config["kvSecretName"];

            }
        }
    }

The AzureKeyVaultEndpoint is passed in the local.settings.json for local env and in the AppConfig for the Azure env.

Anthony C
  • 1,972
  • 2
  • 14
  • 26