I am using the code below to try to delete a specific message from a deadletter queue of a subscription. I am using PeekBySequenceNumberAsync to get to the specific message. The issue is that that method evidently is not setting a lock token. I am getting the following error: "The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue, or was received by a different receiver instance."
Any idea on how I can delete a specific message from a subscription deadletter queue? I am using .net core Microsoft.Azure.ServiceBus Library.
public async Task<bool> DeleteMessage(long sequenceNumber, string topicPath, string subscriptionName, bool deadLettered = false)
{
bool success = false;
string connectionString = Environment.GetEnvironmentVariable("SB_CONNECTION_STRING");
MessageReceiver receiver = null;
try
{
string path = EntityNameHelper.FormatSubscriptionPath(topicPath, subscriptionName);
if (deadLettered)
path = EntityNameHelper.FormatDeadLetterPath(path);
receiver = new MessageReceiver(connectionString, path, ReceiveMode.PeekLock );
var message = await receiver.PeekBySequenceNumberAsync(sequenceNumber);
// If we have found the message
if (message != null)
{
await receiver.CompleteAsync(message.SystemProperties.LockToken);
success = true;
}
else
{
Console.WriteLine("Message with sequence number: " + sequenceNumber.ToString() + " was not found");
}
}
catch (ServiceBusException e)
{
if (!e.IsTransient)
{
Console.WriteLine(e.Message);
}
}
finally
{
if (receiver!=null)
await receiver.CloseAsync();
}
return success;
}