Azure / azure-functions-servicebus-extension

Service Bus extension for Azure Functions
MIT License
65 stars 35 forks source link

Using a run-time queue name for a service bus trigger #206

Closed jdaaboul closed 1 year ago

jdaaboul commented 1 year ago

The service bus trigger can currently use the %queue-name% notation to be able to get a queue name binding to an appsetting file.

However, I would like to control in which queue a service bus trigger is listening at runtime.

For example, I would like to create a queue for each developer using his machine name, and then I would like the service bus trigger to use a dynamically calculated queue name in the attribute.

This doesn't look supported, am I missing something.

This is what I've tried:


[FunctionName(nameof(DriverServiceBusTrigger))]
        public async Task RunAsync([ServiceBusTrigger("queue-name", Connection = "ServiceBus:ConnectionString")] ServiceBusReceivedMessage[] myQueueItems, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItems}");
}

In order for queue name to be dynamic, i tried to use bindings without any success

 [FunctionName(nameof(ProcessMessage))]
        public async Task Run([ServiceBusTrigger("myqueue-items-source-binder2", Connection = "ServiceBus:ConnectionString")] string myQueueItem, Binder binder, ILogger log)
        {
            string queueName= ("queue-name" + Environment.MachineName).ToLowerInvariant();

            var attributes = new Attribute[]
            {
                new ServiceBusAccountAttribute("ServiceBus:ConnectionString"),
                new ServiceBusAttribute(queueName),
            };

            string message = await binder.BindAsync<string>(attributes);

        }
shreyabatra4 commented 1 year ago

Function runtime can only use static naming for resources. You can try leveraging the Service Bus SDK directly to have dynamic naming.