Search code examples
c#azure-functions.net-8.0azure-functions-isolatedazure-configuration

Azure Function Configuration values not available in function attributes


I have an Azure Function running in Azure Portal. Its configuration resides in a configuration resource, some values are in a key vault resource. The Azure Function was implemented in .NET 6 with the in-process model. I migrated the code to .NET 8 using the isolated-worker model.

Here's my Program.cs code:

using System.Reflection;
using Azure.Identity;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace My.AzureFunctions
{
    public class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWebApplication()
                .ConfigureAppConfiguration(config =>
                {
                    var builtConfig = config.Build();
                    var appConfigurationName = builtConfig
                    
                    var defaultAzureCredentialOptions = new DefaultAzureCredentialOptions
                    {
                        ExcludeAzureCliCredential = false,
                        ExcludeAzurePowerShellCredential = true,
                        ExcludeEnvironmentCredential = false,
                        ExcludeManagedIdentityCredential = false,
                        ExcludeVisualStudioCodeCredential = true,
                        ExcludeVisualStudioCredential = true
                    };
                    config.AddAzureAppConfiguration(options =>
                    {
                        options.Connect(new Uri($"https://{appConfigurationName}.azconfig.io"),
                        new DefaultAzureCredential(defaultAzureCredentialOptions))
                            .ConfigureKeyVault(kvOptions =>
                            {
                                kvOptions.SetCredential(
                                    new DefaultAzureCredential(defaultAzureCredentialOptions));
                                kvOptions.SetSecretRefreshInterval(TimeSpan.FromHours(1));
                            });
                    });
                    config.AddEnvironmentVariables();

                    foreach (var kvp in config.Build().AsEnumerable())
                    {
                        if (!string.IsNullOrEmpty(kvp.Value))
                        {
                            Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
                        }
                    }
                })
                .ConfigureServices((context, services) =>
                {
                    // Bind configurations to options
                    // ...
                })
                .Build();

            host.Run();
        }
    }
}

The Functions look like this:

    [Function("TestTimerTrigger")]
    public void TestTimerTrigger(
        // This causes an error
        [TimerTrigger("%TimeValue%)] TimerInfo timerInfo,
        FunctionContext functionContext,
        CancellationToken cancellationToken)
    {
        // Here TimeValue residing in Azure Configuration can be read.
        _logger.LogInformation("TestTimerTrigger function executed with: {TimeValue}", _configuration["TimeValue"]);

        // Also accessible here
        _logger.LogInformation("TestTimerTrigger function executed with: {TimeValue}", Environment.GetEnvironmentVariable("TimeValue"));
    }

    [Function("MyEventTrigger")]
    // The value cannot be read here, either.
    public async Task Run([EventHubTrigger("", Connection = "MyEventHub:ConnectionString")] EventData[] messages, 
        FunctionContext functionContext)
    {
        // ...
    }

I get these errors:

The 'TestTimerTrigger' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.TestTimerTrigger'. Microsoft.Azure.WebJobs.Host: '%TimeValue%' does not resolve to a value.

Microsoft.Azure.WebJobs.Extensions.EventHubs: EventHub account connection string with name 'MyEventHub:ConnectionString' does not exist in the settings. Make sure that it is a defined App Setting.

In the old .NET 6 in-process model Function I could access the configuration values in the Function Attributes, but after changing the code to .NET 8 and the isolated worker model, I have the described problem. The configuration itself and the way to access it has not changed. What am I doing wrong?


Solution

  • I too faced similar issue and AFAIK, in isolated the values dont show up in app setting while setting up by Program.cs. Rather using the code in Program.cs to connect to Azure App Configuration, You can directly add the app configuration key(without value) in app setting as below :

    enter image description here

    Here I am adding :

    @Microsoft.AppConfiguration(Endpoint=https://rithwik98.azconfig.io; Key=rithcron)

    Here rithcron is keyname in App Configuration Service and rithwik98 is the name of service:

    enter image description here

    Now the function app runs:

    enter image description here

    To make this work you need to enable managed identity of function app and give App Configuration Data Reader/owner role to function app managed identity