Skip to content

Instantly share code, notes, and snippets.

@anderbakk
Last active March 22, 2022 13:03
Show Gist options
  • Save anderbakk/1a342c3486f83ac0cfebc2e2b2a5f14d to your computer and use it in GitHub Desktop.
Save anderbakk/1a342c3486f83ac0cfebc2e2b2a5f14d 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/
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;
}
}
}
@jstallm
Copy link

jstallm commented Aug 28, 2019

Can you please upload the azure function which invokes this? Id like to see how this method is invoked exactly?
Thank you in advance. I hope this gist still lives on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment