Azure Service Bus .NET SDK Deep Dive – Forwarding

Introduces the concept of forwarding from one entity to another, for more posts in this series go to Contents.

Queues and Subscriptions (more about those later) have the possibility to become what I call “forwarding-only” entities. An entity can be set into the forwarding mode by setting the property ForwardTo to a destination.

var description = new CreateQueueOptions("Hop");
await client.CreateQueueAsync(description);
description = new CreateQueueOptions("Hop0");
await client.CreateQueueAsync(description);
description = new CreateQueueOptions("Hop1") { ForwardTo = "Hop0" };
await client.CreateQueueAsync(description);
description = new CreateQueueOptions("Hop2") { ForwardTo = "Hop1" };
await client.CreateQueueAsync(description);
description = new CreateQueueOptions("Hop3") { ForwardTo = "Hop2" };
await client.CreateQueueAsync(description);
description = new CreateQueueOptions("Hop4") { ForwardTo = "Hop3" };
await client.CreateQueueAsync(description);

The code above creates a series of queues that route messages from one queue to another. So when sending a message to any queue between Hop1 and Hop4 they will all end up in Hop0 queue due to the forwarding rules defined.

await using var serviceBusClient = new ServiceBusClient(connectionString);
var sender = serviceBusClient.CreateSender("Hop4");
var message = new ServiceBusMessage("Weeeeeeehhh!");
await sender.SendMessageAsync(message);

Let’s see what happens if we send a message to Hop4 is demonstrated in the code above.

Now let’s see what happens if we setup the forwarding from Hop0 to Hop.

It is still possible to send messages as before to Hop4 but when we try to receive from Hop0 that we changed from a regular entity to a forwarding entity the following happens:

Once an entity is set to forwarding mode it is no longer possible to receive from them. Any attempt will throw an exception telling us “Cannot create a message receiver on an entity with auto-forwarding enabled”. But what happened to the message we sent to Hop4?

When messages pass through more than four queues or topics chained together with forwarding, they will be sent to the last destination dead-letter queue. If the destination queue or topic is disabled, deleted or it exceeds the maximum entity size (quota exceeded), the message will also be moved to the last destination dead-letter queue.

It is worth to note that Service Bus bills one operation for each forwarded message. When auto-forwarding is set up, the value for AutoDeleteOnIdle on both the Source and the Destination is automatically set to the maximum value of the data type.

Updated: 2021-06-25 to use the new SDK

About the author

Daniel Marbach

1 comment

Recent Posts