Search code examples
azureazure-functionsazure-functions-runtimeazure-app-configurationazure-functions-isolated

How to use Azure App Configuration Service with Azure Functions in local dev environment?


I am writing an Azure Function (Service Bus Topic Triggered Function in isolated mode) which is part of a bigger solution that has API and other projects. The API reads the configuration settings from Azure App Configuration Service and that is working fine.

What I want to do is read the settings in my Function from there and set the bindings for the Function based on those settings however it is not working.

Here's what I did:

  • I created an App Configuration Service in my Azure Subscription and added the configuration keys there.

enter image description here

  • Then I added the reference to this App Configuration Service in Program.cs using the code like below:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureAppConfiguration(builder =>
    {
        builder.AddAzureAppConfiguration(
            "Endpoint=https://myfunctionconfig.azconfig.io;Id=somerandomstring;Secret=otherrandomstring",
            optional: false)
            .AddEnvironmentVariables()
        .Build();
    })
    .ConfigureFunctionsWorkerDefaults()
    .Build();

host.Run();
  • My Function code looks something like this:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace ServiceBusTriggeredFunction;

public static class ServiceBusTopicTrigger
{
    [Function("ServiceBusTopicTrigger")]
    public static void Run([ServiceBusTrigger("%FunctionsConfiguration:ServiceBusTopicTrigger:Topic%", "%FunctionsConfiguration:ServiceBusTopicTrigger:Subscription%", Connection = "FunctionsConfiguration:ServiceBusTopicTrigger:Connection")] string mySbMsg,
        FunctionContext context)
    {
        var logger = context.GetLogger("ServiceBusTopicTrigger");
        logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
    }
}

However when I run the code locally, I am getting the following error:

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

Basically the issue is that it is not able to read the settings value (FunctionsConfiguration:ServiceBusTopicTrigger:Topic).

However the same code works flawlessly if I define the same settings in local.settings.json.

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "FunctionsConfiguration:ServiceBusTopicTrigger:Connection": "Endpoint=sb://mynamespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=myaccesskey=",
        "FunctionsConfiguration:ServiceBusTopicTrigger:Topic": "topic",
        "FunctionsConfiguration:ServiceBusTopicTrigger:Subscription": "subscription"
    }
}

I am wondering if someone can tell me what I am doing wrong here and if there's a way for me to read the settings from an App Configuration Service (I guess the same would apply to appsettings.json as well) and set the Function bindings based on the settings.


Solution

  • I asked the same question on Github here - https://github.com/Azure/azure-functions-dotnet-worker/issues/1437 and this is the response I got there:

    worker configuration sources are not currently supported by extensions/bindings, as that configuration must be available at the app/platform level (platform components depend on that configuration).

    Currently, your best option would be to use the feature described here: https://learn.microsoft.com/en-us/azure/app-service/app-service-configuration-references