Azure Service Bus .NET SDK Deep Dive – Topologies

Shows how to create topologies by combining subscriptions with forwarding, for more posts in this series go to Contents.

In the previous post we looked at how we can leverage topics and subscriptions together with rules to create powerful message-based solutions.

A topic subscription resembles a virtual queue that receives copies of the messages that are sent to the topic. Messages are received from a subscription identically to the way they are received from a queue.

The implication of the above behavior is that when a subscriber has an input queue and multiple subscriptions, it needs to receive from the input queue as well as from all the subscriptions. Managing those message receivers can get complex quite quickly. A different approach that significantly simplifies the receiving logic of a subscriber is to leverage the already shown auto-forwarding so that all messages in the subscriptions of the subscriber will be automatically forwarded into the input queue of the subscriber.

So if we use exactly the same code as in the previous example with the rush and currencySubscription but enable forwarding to the input queue like shown below, it is no longer needed to receive messages from both subscriptions because all messages are auto-forwarded into the input queue.

var subscriptionDescription = new CreateSubscriptionOptions(topicName, rushSubscription)
{
    ForwardTo = inputQueue
};
await client.CreateSubscriptionAsync(subscriptionDescription);

subscriptionDescription = new CreateSubscriptionOptions(topicName, currencySubscription)
{
    ForwardTo = inputQueue
};
await client.CreateSubscriptionAsync(subscriptionDescription);

And here is the straight forward receiver receiving from the input queue which for the sake of this example just receives two messages from the input queue.

await using var receiver = serviceBusClient.CreateReceiver(inputQueue);
var receivedMessages = await receiver.ReceiveMessagesAsync(2);
foreach (var receivedMessage in receivedMessages)
{
    var body = Encoding.UTF8.GetString(receivedMessage.Body);
    var label = receivedMessage.Subject;
    receivedMessage.ApplicationProperties.TryGetValue("currency", out var currency);
}

And here the demo of the code above executed.

And the picture in Service Bus Explorer looks the same in case we have no receiver connected to it.

For another in-depth look at topologies I can also recommend to read

Updated: 2021-03-23 to use the new SDK

About the author

Daniel Marbach

1 comment

By Daniel Marbach

Recent Posts