5

Assume the following typical Queue Trigger function:

public void Run([QueueTrigger("queue1")]object data, ILogger log)
{
    // Do something with data
}

My problem is that "queue1" has to be a constant field, so it has to be defined at compile time. Also, I'd want to have a base class for Queue Triggers, that could work like this:

public abstract class QueueBase<TModel>
{
    public void Run([QueueTrigger("queueName")]TModel data, ILogger log)
    {
        // Do something with data, log something etc.
        OnRunExecuted(data);
        // Do something with data, log something etc.
    }

    public abstract void OnRunExecuted(TModel data);
}

with this, I could write own classes which inherit from QueueBase but can even live inside a library which doesn't have Microsoft.Azure.WebJobs dependency:

public class MyQueueHandler : QueueBase<MyModel>
{
    public void OnRunExecuted(MyModel data) => ...;
}

But it's impossible to pass in a Queue name... is it?

thmshd
  • 5,729
  • 3
  • 39
  • 67

2 Answers2

2

See binding expressions:

In short, you can pass in a variable queue name as "%queue-name-variable%"

[FunctionName("QueueTrigger")]
public static void Run(
    [QueueTrigger("%queue-name-variable%")]string myQueueItem, 
    ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
}

Where input-queue-name is defined in your configuration like

{"queue-name-variable": "queue-name-in-current-env"}
unie
  • 143
  • 10
  • 2
    Thanks for your contribution, I understand there is that option. I want the classes I write to be more flexible though, and having "queue-name-variable" always map to one specific other value (per environment) isn't very helpful. And the problem with the `INameResolver` is, that it doesn't provide any context of resolution, so within method `Resolve (string name)` you don't have much to decide upon. – thmshd Oct 02 '20 at 07:17
  • Exactly, writing trigger function for each queue is so arcane. Several requests are made asking for dynamic handling of queue name in the trigger function. You can also upvote my request [here](https://feedback.azure.com/d365community/idea/6ec830d8-b3b9-ed11-a81b-000d3ae6a6aa) – Anand Sowmithiran Mar 03 '23 at 11:39
1

As i remember attribute QueueTrigger accept only const string, so you can try make some tricks using environment variables like in post how to pass dynamic queue name

Ivan Martinyuk
  • 1,230
  • 1
  • 9
  • 13