Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Azure App Config Service with Azure Functions for binding input parameters #1437

Closed
gmantri opened this issue Mar 24, 2023 · 1 comment
Assignees

Comments

@gmantri
Copy link

gmantri commented Mar 24, 2023

Cross posting from Stack Overflow: https://stackoverflow.com/questions/75815924/how-to-use-azure-app-configuration-service-with-azure-functions-in-local-dev-env.

I also found a similar issue on Microsoft Q&A: Azure Function - CosmosDbTrigger - Bind variables from Keyvault and App configuration instead of appsettings but the solution provided there is not working.


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.

@ghost ghost assigned brettsam Mar 24, 2023
@kshyju kshyju transferred this issue from Azure/azure-functions-host Mar 24, 2023
@fabiocav
Copy link
Member

@gmantri 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

Hope this helps!

@Azure Azure locked as resolved and limited conversation to collaborators Apr 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants