.NET – Clear a blue service bus queue

We use the Service Bus queue in our project. When the administrator chooses to clear the queue, we need a function to delete all messages in the queue. I searched the Internet but couldn’t find it To any function that does this in the QueueClient class.

I have to pop up all the messages one by one and then mark them as clearing the queue completely, or is there a better way?

QueueClient queueClient = _messagingFactory.CreateQueueClient(
queueName, ReceiveMode.PeekLock);

BrokeredMessage brokeredMessage = queueClient.Receive();< br />
while (brokeredMessage != null )
{
brokeredMessage.Complete();
brokeredMessage = queueClient.Receive();
}

using the Receive() method in a while loop just like you will cause the code to run indefinitely when the queue is empty , Because the Receive() method will wait for another message to appear in the queue.

If you want to make it run automatically, try the Peek() method.

For example:

while (queueClient.Peek() != null)
{
var brokeredMessage = queueClient.Receive();
brokeredMessage.Complete ();
)

You can use ReceiveMode.ReceiveAndDelete as mentioned by Hocho to make it easier again.

We are here We use service bus queues in our project. When the administrator chooses to clear the queue, we need a function to delete all messages in the queue. I searched online, but I couldn’t find any function to do this in the QueueClient class. < p>

Do I have to pop up all the messages one by one and then mark them as a complete clear queue, or is there a better way?

QueueClient queueClient = _messagingFactory.CreateQueueClient(
queueName, ReceiveMode.PeekLock);

BrokeredMessage brokeredMessage = queueClient.Receive();< br />
while (brokeredMessage != null )
{
brokeredMessage.Complete();
brokeredMessage = queueClient.Receive();
}

Using the Receive() method in a while loop just like you will cause the code to run indefinitely when the queue is empty, because the Receive() method will wait for another message to appear In the queue.

If you want to make it run automatically, please try the Peek() method.

For example:

< pre>while (queueClient.Peek() != null)
{
var brokeredMessage = queueClient.Receive();
brokeredMessage.Complete();
}

You can use ReceiveMode.ReceiveAndDelete as mentioned by Hocho to make it easier again.

Leave a Comment

Your email address will not be published.