2

I am trying to connect my function app to keyvault and get queue name and connection secrets. This was working well with .netcore3.1 app using the ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder) method in FunctionStartup.

After upgrading to .net5 dotnet-isolated, the bindings does not work. I configured azurekeyvault in Program.cs but still it does not pick from keyvault.

QueueFunction

 public static void Run([QueueTrigger("%QueueName%", Connection = "QueueConnection")] string message, string id)
    

Startup.cs (.netcore3.1)- working

  public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        var azureKeyVaultURL = Environment.GetEnvironmentVariable("AzureKeyVaultURL");
        var azureKeyVaultADAppID = Environment.GetEnvironmentVariable("AzureKeyVaultMIAppID");


        builder.ConfigurationBuilder
                    .SetBasePath(Environment.CurrentDirectory)
                    .AddAzureKeyVault(new Uri(azureKeyVaultURL), new ManagedIdentityCredential(azureKeyVaultADAppID))
                    .AddEnvironmentVariables()
                .Build();
    }

Program.cs (.net5)- Not working

var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureAppConfiguration(config =>{
                 var azureKeyVaultURL = Environment.GetEnvironmentVariable("AzureKeyVaultURL");
                var azureKeyVaultADAppID = Environment.GetEnvironmentVariable("AzureKeyVaultMIAppID");

                config
                   .SetBasePath(Environment.CurrentDirectory)
                   .AddAzureKeyVault(new Uri(azureKeyVaultURL), new ManagedIdentityCredential(azureKeyVaultADAppID))
                   .AddEnvironmentVariables()
                .Build();
            })
Prabhu AP
  • 71
  • 4
  • I'm still investigating a similar issue, but basically I think that whats happening is that you're only configuring the isolated process, not the host process and the trigger bindings are either running in the host process or using IPC to get the configuration from the host process. – justin.m.chase May 10 '22 at 18:00
  • I am having the same issue in the isolated azure function. Did you find the solution for this? – Tushar patel May 17 '22 at 10:29

2 Answers2

0

you need to use another overload of ConfigureAppConfiguration method:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureAppConfiguration((hostBuilder, config) =>
    {
        if (hostBuilder.HostingEnvironment.IsProduction())
        {
            var builtConfig = config.Build();
            var secretClient = new SecretClient(new Uri($"https://{builtConfig["KeyVaultName"]}.vault.azure.net/"), new DefaultAzureCredential());
            config.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
        }
    })
    .Build();
Boris Lipschitz
  • 9,236
  • 5
  • 53
  • 63
-1

Here is the HostBuilder pipeline sample from official document, notice that the Build function:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(s =>
    {
        s.AddSingleton<IHttpResponderService, DefaultHttpResponderService>();
    })
    .Build();
Doris Lv
  • 3,083
  • 1
  • 5
  • 14
  • Hi Doris, I had added Build() already. As per my understanding, the inbuilt function-startup methods are called before the Program.cs. Thus the function binding is not happeing – Prabhu AP Apr 23 '21 at 10:54
  • 1
    I don't think so.. You could test with this official sample, and I would search for more information about this. https://github.com/Azure/azure-functions-dotnet-worker/tree/main/samples/FunctionApp – Doris Lv Apr 26 '21 at 05:51
  • @PrabhuAP - Are you trying to use both Startup.cs and Program.cs with .net 5? – Mike Becatti Apr 27 '21 at 18:14
  • @MikeBecatti Basically I want the function app to pickup the connection string and queue name from keyvault. With .net 5, program.cs will be invoke only after the host initializes the function apps using the metadata. At that point, since keyvault is not yet invoked, the binding will not work. – Prabhu AP Apr 28 '21 at 12:14
  • @PrabhuAP Any luck making that work? I want to implement user secrets and keyvault and noticed the same issue. I'm stuck. – Eddy Castillo Sep 09 '21 at 03:48