2
votes

Using the template to create a azure function, one can create function only listening to particular pair of azure topic/subscription:

{
  "bindings": [
    {
      "name": "mySbMsg",
      "type": "serviceBusTrigger",
      "direction": "in",
      "topicName": "ftopic1",
      "subscriptionName": "mysub",
      "connection": "collosysazfuncsb_RootManageSharedAccessKey_SERVICEBUS",
      "accessRights": "Manage"
    }
  ],
  "disabled": false
}

and then in run.csx you just receive the message

public static void Run(string message, TraceWriter log)
{
    log.Info($"message: {message}");
}

Is there a way to listen to any topic/subscription using azure function and then receive topicName & subscriptionName as parameters in Run method.

Doing topic-name as * does not help, and also it does not provide topic-name in Run.

1
Azure Service Bus (ASB) didn't allow for subscribers to subscribe more than one topic at a time. In other words, the ASB doesn't have a topic-based wildcards like for example the IBM WebSphere MQ. However, the ASB has a feature known as auto-forwarding, where the messages can be forwarded within the same namespace from any topics to any one based on the filter, etc.Roman Kiss

1 Answers

3
votes

Azure Functions only allows to listen to a single queue or subscription. It doesn't allow to listen to multiple entities (queues or subscriptions) as the Azure Service Bus client doesn't support this. Instead, as it was pointed out, you could leverage Auto Forwarding feature of Azure Service Bus. The broker will forward any messages to the destination topic/queue and you'll have a single queue for Azure Function to feed of.

It's important to note that auto forwarded messages will not carry any information that would allow to identify from what queue/subscription they have originated. This is only possible with dead-lettered messages.

Since you're interested in topics, you could "workaround" this issue by having an action on the rules of your subscriptions that would stampt messages with a custom property. For example, having 3 topics with default subscription each and a default filter with rule action

set [x-source] = 'topic-N'

where N is a topic identifier, would cause all auto forwarded messages to contain x-source custom property with value corresponding to the topic they have originated from.