Azure:如何从服务总线队列中删除 "DeadLettered" 条消息

Azure: How to Delete "DeadLettered" Messages from Service Bus queue

我想从服务总线队列中删除 Dead Lettered 消息。 特别是,此值称为 DeadLetterMessageCount,您可以通过右键单击 "Properties" 项目的服务器资源管理器中的 SB 队列(在使用 SB 队列的情况下)。

我想这样做的原因是因为我已经设置了云服务的自动缩放。因此,当 SB 队列非常大时,它会添加更多的核心以便更快地处理消息(它启用更多的工作角色)。我意识到,当您根据队列中的消息数量设置缩放时,它也会计算 DeadLettered 消息(无法使用的消息)。 所以这是浪费金钱,因为启用了更多不需要的实例。

如有任何疑问,请告诉我。

感谢您的帮助

您从死信队列中读取和删除消息的方式与从普通队列或订阅中读取的方式相同。

您可以使用此方法获取队列的路径:QueueClient.FormatDeadLetterPath(queuePath).

另请参阅之前的回答:How do I delete a DeadLetter message on an Azure Service Bus Topic

这是从队列中删除死信消息的代码。

public async void DeleteMessagesFromQueueAsync()
    {
        bool isDeadLetter=true;
        long SequenceNumber = 12;
        string queuePath='queue name';
        string connectionString='connection string of ASB Namespace';

        BrokeredMessage _srcMessage = null;
        DeleteMessageResponse _msgDeletionStatus = new DeleteMessageResponse();
        MessageReceiver fromQueueClient = null;
        try
        {
            MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);

            string _fromEntityPath = !isDeadLetter ? queuePath : QueueClient.FormatDeadLetterPath(queuePath);

            fromQueueClient = await factory.CreateMessageReceiverAsync(_fromEntityPath, ReceiveMode.PeekLock);

                BrokeredMessage _message = await fromQueueClient.ReceiveAsync(SequenceNumber);
                if (_message != null)
                    _srcMessage= _message;

            if (_srcMessage != null )
            {  
                    await _srcMessage.CompleteAsync();
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (fromQueueClient != null)
                await fromQueueClient.CloseAsync();
        }
    }

您可以使用'ReceiveAndDelete'模式和'ReceiveBatchAsync'从DeadLetterQueue中快速删除

private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var DLQPath = "/$DeadLetterQueue"; ///**** Important - Pointing to DLQ'

                var topicName = "message";
                var sub = "message-subscription";
                int batchSize = 100;

                runProcess = true;

                _subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionSt, topicName, sub + DLQPath, ReceiveMode.ReceiveAndDelete);

                int cnt = 0;

                do
                {
                    var messages = await _subscriptionClient.ReceiveBatchAsync(batchSize);
                    var msgCount = messages.Count();

                    if (msgCount == 0)
                    {
                        break;
                    }
                    cnt += msgCount;
                    labelCount.Text = cnt.ToString();
                }
                while (runProcess);

                _subscriptionClient.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().Message);
                return;
            }
        }