Move messages in Service Bus Queue

This post would demonstrate how to move the messages between two service bus queues. This is a useful tool to take backup of the messages during production deployment, system outage, moving the messages to different queue to manage the huge load in service bus queue etc. Before I show how to do, let me give a quick introduction of the service bus queue.

Service bus queue offers simple first in, first out guaranteed message delivery, highly available with support for enterprise level messaging. Microsoft also offers Azure REST queue as a messaging service for distributed environment and the difference are outlined here. The message that comes in and goes out from service bus queue is called brokered message and it stores inside the service bus queue in serialized format. The format could be JSON, binary, xml etc. which can be set during the send operation to the service bus queue. This tool retains the serialized message and move the same message to a different queue or a deadletter queue. Following code snippet is a simple console application which does the message movement between source and destination queue.

Before running the code, make sure to add the service bus queue package from nugget repository. If you are accessing the message before sending it, you have to create a clone of the message because brokered message cannot be used in case the body of the message is already accessed(code is commented). Deadletter queue can be used as a destination queue and can be referred as {queue_name}/$deadletterqueue