Skip to content
New issue

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

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

Already on GitHub? # to your account

How can I use Azure App Configuration for QueueTrigger bindings? #9598

Open
gpuchtel opened this issue Oct 10, 2023 · 6 comments
Open

How can I use Azure App Configuration for QueueTrigger bindings? #9598

gpuchtel opened this issue Oct 10, 2023 · 6 comments
Assignees
Labels

Comments

@gpuchtel
Copy link

Library name and version
Microsoft.Azure.Functions.Worker 1.19.0

Query/Question
I've been searching for a way to use Azure 'App Configuration' to store the connection string of an Azure Storage Queue. After hours searching on the Web I'm even more confused--there seems to be conflicting opinions on how to do this. Maybe even using Managed Identity, but that makes debugging locally a nightmare.

What I want is something like this:

[Function("foo")
public void foo([QueueTrigger("bar", Connection = "FooConnection")] QueueMessage message)

Where "FooConnection" is the name of a 'key' in an Azure App Configuration from which the connection string is specified. adly, it seems that Azure only looks in the 'Configuration' of the function (or local settings if running locally).

So, is this even possible?

Note: I do 'AddAzureAppConfiguration(..) in my HostBuilder.

Thanks in advance,

I've tried to surround the name with '%' like "%FooConnection%", but I get an error saying the expression did not evaluate to a value.

I've seen posts that speak so an expression pattern, but there is not much guidance on it.

see: https://stackoverflow.com/q/77180419/8307483

@bhagyshricompany
Copy link

Thanks for informing .
have you tried like this using Microsoft.Extensions.Configuration;

public static class ConfigurationSingleton
{
private static readonly IConfiguration _configuration;

static ConfigurationSingleton()
{
    _configuration = new ConfigurationBuilder()
        .AddAzureAppConfiguration()
        .Build();
}

public static string GetConnectionString()
{
    return _configuration["FooConnection"];
}

}

[Function("foo")]
public void Foo([QueueTrigger("bar")] QueueMessage message)
{
string connectionString = ConfigurationSingleton.GetConnectionString();
// Use the connection string
}

@gpuchtel
Copy link
Author

No. I want the connection string to come from the attribute whereby the connection string 'should' come from my App Configuration using that name.

@gvajda
Copy link

gvajda commented Oct 19, 2023

This is just a lead: I remember reading that Event-based scaling doesn't allow dynamic elements in the function signature, all queues, databases, etc need to be hardcoded, constant, or app setting reference.
However, it is possible when Runtime-based scaling is enabled (Configuration --> Function Runtime Settings --> Runtime Scale Monitoring). This setting has conditions and affects a wider range of behaviour so check if that matches your use case.

@gpuchtel
Copy link
Author

@gvajda thanks for the replay. Not to nit-pick, but (in my view), it is an 'app setting', just that the setting is put in an Azure Configuration. I am able to resolve other configuration settings in this way, just not in an attribute definition. Sadly, this makes using 'Bicep' as a deployment tool almost useless (in my case).

@gvajda
Copy link

gvajda commented Oct 20, 2023

@gpuchtel 'app settings' in Azure Function App context means a very specific thing, it's not a matter of opinion. If you want to use dynamic values (including values fetched from App Config and cached in fnapp memory) then my answer applies. Unclear how this relates to bicep.

@erenken
Copy link

erenken commented Jan 14, 2025

This just seems like a security and maintenance issue. I will admit I haven't looked to see how or if it is possible to point to a specific Queue connection string using a service principal. I want all my connection information stored in 1 place. Azure App Config and there a pass through to Key Vault using the Identity my App Config service is configured with.

builder.Build().Run() and then it should use the values in IConfiguration at that time; however, it appears to pull those values before .Build().

It really seems like I should be able to setup IConfiguration the way I need it before .Build().

I even tried something crazy like this:

var apiConfiguration = new ConfigurationBuilder().AddMyStandardAppConfig(
    new ConfigurationBuilder()
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build()
    )
    .Build();

apiConfiguration["AzureWebJobsStorage"] = apiConfiguration.GetValue<string>("Attachments:ConnectionString");
apiConfiguration["AzureWebJobsAttachmentsConnectionString"] = apiConfiguration.GetValue<string>("Attachments:ConnectionString");
apiConfiguration["AttachmentsQueueName"] = apiConfiguration.GetValue<string>("Attachments:Queue");

var builder = FunctionsApplication.CreateBuilder(args);

var funcConfiguration = new ConfigurationBuilder()
    .AddConfiguration(apiConfiguration)
    .AddConfiguration(builder.Configuration)
    .Build();

builder.Services.Replace(ServiceDescriptor.Singleton<IConfiguration>(funcConfiguration));

Then in my function I have

public void UploadAttachment([QueueTrigger("%AttachmentsQueueName%", Connection = "AttachmentsConnectionString")] QueueMessage message)

I tried the replace Singleton as I saw other places where it looked like it worked.

I am running .NET 9 isolate-function. I am very sad that this just doesn't work. I was actually trying to step through the .NET function builder code and see if I could find where this is happening and override that part. I still might try, but I have wasted days on this already.

I miss the old name resolver from the first WebJobs SDK.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants