-
-
Save coderpatros/04d55d419269adab71341eaa91f07259 to your computer and use it in GitHub Desktop.
Resend dead letters from Azure Servicebus. Read more on http://www.anderbakk.com/resend-dead-letters-from-an-azure-service-bus-queue/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static async Task ResendDeadLetters(string servicebusConnectionString, string queueName) | |
{ | |
var messageFactory = MessagingFactory.CreateFromConnectionString(servicebusConnectionString); | |
var deadletterReceiver = await messageFactory.CreateMessageReceiverAsync(QueueClient.FormatDeadLetterPath(queueName), | |
ReceiveMode.PeekLock); | |
var sender = await messageFactory.CreateMessageSenderAsync(queueName); | |
const int max = 100; | |
var current = 0; | |
//Run until the Dead Letter Queue is empty or take the max number of messages | |
while (current < max) | |
{ | |
var deadLetter = await deadletterReceiver.ReceiveAsync(TimeSpan.Zero); | |
if (deadLetter != null) | |
{ | |
var newMessage = | |
new BrokeredMessage(deadLetter.GetBody<Stream>()) | |
{ | |
ContentType = deadLetter.ContentType, | |
CorrelationId = deadLetter.CorrelationId | |
}; | |
await sender.SendAsync(newMessage); | |
await deadLetter.CompleteAsync(); //Removes the message from the dead letter queue | |
current++; | |
} | |
else | |
{ | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment