'Is it possible to listen to a service bus with multiple azure functions?

I'm trying to listen to a service bus that can contain messages with multiple event types. When a message arrives I want to process it based on the event type. I have 2 azure functions to handle the different event type messages, but when one function receives the message the other one is not triggered. Is it possible to trigger both of them and let them decide which one processes the message?

Here is some sample code, these are in 2 separate projects:

[FunctionName("Function1")]
public async Task RunAsync(
    [ServiceBusTrigger("queueName", Connection = "connectionName")]
    Message message,
    MessageReceiver messageReceiver,
    ILogger log)
{
    var body = Encoding.Default.GetString(message.Body);
    var messageType = _helper.GetMessageType(body);

    if (messageType is not MessageType.Type1)
        return;

    // business logic
    await messageReceiver.CompleteAsync(message.SystemProperties.LockToken);
}

[FunctionName("Function2")]
public async Task RunAsync(
    [ServiceBusTrigger("queueName", Connection = "connectionName")]
    Message message,
    MessageReceiver messageReceiver,
    ILogger log)
{
    var body = Encoding.Default.GetString(message.Body);
    var messageType = _helper.GetMessageType(body);

    if (messageType is not MessageType.Type2)
        return;

    // business logic

    await messageReceiver.CompleteAsync(message.SystemProperties.LockToken);
}


Solution 1:[1]

With Azure Service Bus Queues, it is not possible as a Queue can only have a single consumer.

A better approach would be to use Azure Service Bus Topics and Subscriptions. You can send the message to a Topic and then create Subscription Filtering rules so that a message goes to an appropriate Subscription.

You can then have 2 Azure Functions each listening to a separate Subscription.

Solution 2:[2]

In addition to Gaurav's answer, you can use the Azure Service Bus ForwardTo feature, see the following example:

enter image description here

Solution 3:[3]

There are few approach and each depends on your requirement.

As Suggested by @Gaurav , Topic is best solution where two subscription to topic ( one is function1 and another function2) and it works most of the case. Also keep in mind that it is individual subscriber to process message and it get failed or success depends on that subscription only and not across subscription.

Now if you still want to stick to Queue then also there is approach. In this you have to pass extra metadata in message like you are doing with type.

 var body = Encoding.Default.GetString(message.Body);
    var messageType = _helper.GetMessageType(body);

    if (messageType is MessageType.Type1)
    { 
        // Call handler/method that process Type1 message.
    }
    else if (messageType is MessageType.Type2)
    {
       // Call handler/method that process Type2 message. 
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gaurav Mantri
Solution 2 Roman Kiss
Solution 3 dotnetstep