I have an Azure functions application (.net8, isolated model). There is a queue trigger working pretty good with internal application storage. It looks like this:
[Function(nameof(InnerStorageTrigger))]
public void InnerStorageTrigger([QueueTrigger("test-queue-inner")] QueueMessage message)
{
_logger.LogInformation($"Inner message: {message.MessageText}");
}
But I also need another trigger, connected to an another storage account. I know I could keep the additional connection string in application settings (either local.settings.json or app configuration in Azure). But unfortunately the connection string comes from an external source and I can only access it in Program.cs. I tried to set an environment variable before initializing the host:
Environment.SetEnvironmentVariable("MyConnection", GetExternalConnectionString());
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.ConfigureAppConfiguration((context, config) =>
{
config.AddEnvironmentVariables();
})
.Build();
host.Run();
The second trigger function:
[Function(nameof(TestQueueOuter))]
public void Run([QueueTrigger("test-queue", Connection = "MyConnection")] QueueMessage message)
{
_logger.LogInformation($"From outer: {message.MessageText}");
}
It does not work. I’m getting an error: "The ‘TestQueueOuter’ function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method ‘Functions.TestQueueOuter’. Microsoft.Azure.WebJobs.Extensions.Storage.Queues: Storage account connection string ‘AzureWebJobsMyConnection’ does not exist. Make sure that it is a defined App Setting.".
I also tried to name the environment variable explicitly "AzureWebJobsMyConnection" – still no success.
So I’d like to know: How can I declare a queue trigger connected to an external storage account, with condition that I can only get the connection string in runtime?
Thank you in advance!
2
Answers
by the time the code in
Program.cs
runs, the triggers have already been indexed. TheQueueTrigger
would have already tried to resolve the connection string from the configuration, and since it’s not yet available, it throws. I think you would need to use the Azure SDK directlyI do agree with @Artur Adam, To add another connection string you can just add in local.settings.json as below:
Function.cs:
Output:
If this does not work for you, use SDK’s to call the queue and do the other functions.