死信队列没有清除

时间:2015-04-14 14:09:54

标签: c# azure servicebus

由于超出重试次数,我有一些自动删除的邮件。现在我正在尝试像这样重新排队:

while ((msg = DeadLetterQueueClient.Receive()) != null)
{
    var body = msg.GetBody<MyModel>();
    QueueClient.Send(new BrokeredMessage(body));
}

我收到所有死信的消息,但是他们没有从队列中清除。每次启动此代码时,都会再次收到相同的消息。

怎么了?

PS:根据旧的Azure门户网站,队列实际上已经关闭了死信。这些消息在Visual Studio Server Explorer中只显示为死信,我也会使用上面的代码接收它们。

4 个答案:

答案 0 :(得分:1)

将邮件标记为已完成,并应将其从死信队列中删除

while ((msg = DeadLetterQueueClient.Receive()) != null)
{
    var body = msg.GetBody<MyModel>();
    QueueClient.Send(new BrokeredMessage(body));
    msg.Complete();
}

答案 1 :(得分:0)

如果要保留标题属性,可能需要使用msg.Clone()而不是将主体复制到新的BrokeredMessage中。

答案 2 :(得分:0)

您还可以尝试使用此代码从队列中删除“不写信”邮件。

  MessageReceiver fromQueueClient = null;

        MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
        fromQueueClient = await factory.CreateMessageReceiverAsync(_entityName, ReceiveMode.PeekLock);

            BrokeredMessage _message = await fromQueueClient.ReceiveAsync(SequenceNumber);

                await _message.CompleteAsync();

答案 3 :(得分:-1)

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.Azure.ServiceBus;
    using Microsoft.Azure.ServiceBus.Core;
    using System.Text;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    namespace ClearDeadLetterQ

{
    [TestClass]
    public class UnitTest1
    {

        const string ServiceBusConnectionString = "Endpoint=sb://my-domain.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=yoursharedaccesskeyhereyoursharedaccesskeyhere";


        [TestMethod]
        public async Task TestMethod1()
        {
            await this.ClearDeadLetters("my.topic.name", "mysubscriptionname/$DeadLetterQueue");
        }

        public async Task ClearDeadLetters(string topicName, string subscriptionName)
        {
            var messageReceiver = new MessageReceiver(ServiceBusConnectionString, EntityNameHelper.FormatSubscriptionPath(topicName, subscriptionName), ReceiveMode.PeekLock);
            var message = await messageReceiver.ReceiveAsync();
            while ((message = await messageReceiver.ReceiveAsync()) != null)
            {
                await messageReceiver.CompleteAsync(message.SystemProperties.LockToken);
            }
            await messageReceiver.CloseAsync();
        }
    }
}