1

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;

    }
evhfla
  • 129
  • 1
  • 7

1 Answers1

0

You shouldn't peek the message but receive it. Peeking operation doesn't acquire the message lock as its intended use is to "browse" the message's content.

Receiving a message by its sequenceNumber was possible in Track 0 SDK. Unfortunately. Looks like Track 1 SDK lost that option.

Anyone finding this question/answer should review the SDK issue.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • But the question is, how do you receive a specific message in .net core? I basically build an application in .net core, where I am displaying messages in the deadletter queue where an admin user has the ability to either delete (complete them) or resubmit them to a retry queue. I know I can receive all of them, until I find the one I am looking for, but that seems very inefficient. – evhfla Sep 11 '20 at 03:38
  • Track 1 has missed this overload. You can raise an issue at https://github.com/Azure/azure-sdk-for-net/issues (I thought it was already raised but couldn't find it). You'd either have to use track 0 (doesn't support .NET Core, [link]( https://learn.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.queueclient.receiveasync?view=azure-dotnet#Microsoft_ServiceBus_Messaging_QueueClient_ReceiveAsync_System_Int64_)) or ask to bring it back in track 1 or the preview of the track 2 SDK [link](https://www.nuget.org/packages/Azure.Messaging.ServiceBus)). – Sean Feldman Sep 11 '20 at 04:04
  • I see you've raised an [issue](https://github.com/Azure/azure-sdk-for-net/issues/15097). I'll update my answer with the comments info. – Sean Feldman Sep 11 '20 at 20:16