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 QueueDescription("Hop");
await client.CreateQueueAsync(description);
description = new QueueDescription("Hop0");
await client.CreateQueueAsync(description);
description = new QueueDescription("Hop1") { ForwardTo = "Hop0" };
await client.CreateQueueAsync(description);
description = new QueueDescription("Hop2") { ForwardTo = "Hop1" };
await client.CreateQueueAsync(description);
description = new QueueDescription("Hop3") { ForwardTo = "Hop2" };
await client.CreateQueueAsync(description);
description = new QueueDescription("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.

var client = new QueueClient(connectionString, "Hop4");
var message = new Message();
message.Body = Encoding.UTF8.GetBytes("Weeeeeeehhh!");
await client.SendAsync(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.

About the author

Daniel Marbach

1 comment

Recent Posts