Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 566
Gaurav Mantri Avatar asked Dec 16 '25 12:12

Gaurav Mantri


2 Answers

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

like image 105
Gaurav Mantri Avatar answered Dec 19 '25 07:12

Gaurav Mantri


As of 2024, this is still not possible, even though Microsoft's documentation here clearly states that you can. The AppConfig configurator loads the values too late for the function worker.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-identity-based-connections-tutorial-2#connect-to-the-service-bus-in-your-function-app

like image 28
Tomislav Markovski Avatar answered Dec 19 '25 06:12

Tomislav Markovski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!