3

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.

2
  • you'll need to make sure that the configuration values are available to the Function when it's running. I Suggest a log or breakpoint to make sure and see if the configuration values are being loaded correctly Commented Mar 22, 2023 at 18:30
  • @1SaeedSalehi - Thanks. You make an interesting point. What I am noticing is that my code in ServiceBusTopicTrigger is executed before the code in Program.cs. That could very well be the reason. How can I ensure that the Function is triggered after the code in Program.cs? Any guidance would be highly appreciated. Commented Mar 22, 2023 at 18:35

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

That linked ticket and page from MS isn't a very helpful answer (comment at them, not you). It talks about references using @Microsoft.AppConfiguration ... and not '%key%'... so exactly how does someone get from using variables with percentage characters (e.g. '%key%') to what ever works?
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.