skip to Main Content

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


  1. by the time the code in Program.csruns, the triggers have already been indexed. The QueueTrigger 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 directly

    Login or Signup to reply.
  2. 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).

    I do agree with @Artur Adam, To add another connection string you can just add in local.settings.json as below:

    {
        "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "rithcon": "DefaultEndpointsProtocol=https;AccountName=rithwikb39a;AccountKey=XZlFEc82NWd0N+AStRDKM3w==;EndpointSuffix=core.windows.net",
        "rithtest": "DefaultEndpointsProtocol=https;AccountName=rithwika043;AccountKey=W1+dvK7ab+AStxb4Huw==;EndpointSuffix=core.windows.net"
      }
    }
    

    Function.cs:

    using System;
    using Azure.Storage.Queues.Models;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.Logging;
    
    namespace FunctionApp213
    {
        public class Function1
        {
            private readonly ILogger<Function1> ri_lg;
    
            public Function1(ILogger<Function1> logger)
            {
                ri_lg = logger;
            }
    
            [Function(nameof(Function1))]
            public void Run([QueueTrigger("rithq", Connection = "rithcon")] QueueMessage message)
            {
                ri_lg.LogInformation($"Queue trigger from queue 1, Msg is : {message.MessageText}");
            }
        }
        public class Function2
        {
            private readonly ILogger<Function2> ri_lg;
    
            public Function2(ILogger<Function2> logger)
            {
                ri_lg = logger;
            }
    
            [Function(nameof(Function2))]
            public void Run([QueueTrigger("rithq2", Connection = "rithtest")] QueueMessage message)
            {
                ri_lg.LogInformation($"Queue trigger from queue 2, Msg is: {message.MessageText}");
            }
        }
    }
    

    Output:

    enter image description here

    If this does not work for you, use SDK’s to call the queue and do the other functions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search