1

我正在使用下面的代码尝试从订阅的死信队列中删除特定消息。我正在使用 PeekBySequenceNumberAsync 来获取特定消息。问题是该方法显然没有设置锁定令牌。我收到以下错误:“提供的锁无效。锁已过期,或者消息已从队列中删除,或者由不同的接收器实例接收。”

关于如何从订阅死信队列中删除特定消息的任何想法?我正在使用 .net 核心 Microsoft.Azure.ServiceBus 库。

    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;

    }
4

1 回答 1

0

你不应该偷看消息,而是接收它。Peeking 操作不会获取消息锁,因为它的预期用途是“浏览”消息的内容。

sequenceNumberTrack 0 SDK中可以通过它接收消息。很遗憾。看起来 Track 1 SDK 失去了该选项。

任何找到此问题/答案的人都应该查看SDK 问题

于 2020-09-10T21:47:51.140 回答