Neo
Neo

Reputation: 16239

How should i read more number of messages from dead letter using .net core c#?

I have below code which works fine but when function got trigger it only read one message at a time.

to get more than 1 message i have given count 10 into ReceiveAsync(10) , but still getting only one message.

public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    try
    {

        var deadQueuePath = EntityNameHelper.FormatDeadLetterPath("demo/subscriptions/demo");

        MessageReceiver deadletterReceiver = new MessageReceiver(Environment.GetEnvironmentVariable("ConnectionStringSettingName"), deadQueuePath, ReceiveMode.PeekLock,RetryPolicy.Default);
        MessageSender sender = new MessageSender(Environment.GetEnvironmentVariable("ConnectionStringSettingName"), "demo",RetryPolicy.Default);

        var deadLetter = await deadletterReceiver.ReceiveAsync(10);
        if (deadLetter != null)
        {
            log.LogInformation($"got new message");

            Message newMessage = new Message(deadLetter.Body)
            {
                ContentType = deadLetter.ContentType,
                CorrelationId = deadLetter.CorrelationId                          
            };

            //Send the message to the Active Queue
            await sender.SendAsync(newMessage);
            await deadletterReceiver.CompleteAsync(item.SystemProperties.LockToken); //Unlock the message and remove it from the DLQ

        }
    }
    catch (Exception ex)
    {
        log.LogInformation($"Exception: {ex}");
    }
}

Upvotes: 1

Views: 956

Answers (1)

Anish K
Anish K

Reputation: 818

ReceiveAsync(10) - This method is used for setting the sequence number of the message to receive and not the count. Look at the details on ReceiveAsync here

In order to fetch multiple messages, you need to set Prefetch property to the receiver.

Make use of the below constructor to initialize your MessageReceiver Class:

public MessageReceiver (Microsoft.Azure.ServiceBus.ServiceBusConnection serviceBusConnection, string entityPath, Microsoft.Azure.ServiceBus.ReceiveMode receiveMode = Microsoft.Azure.ServiceBus.ReceiveMode.PeekLock, Microsoft.Azure.ServiceBus.RetryPolicy retryPolicy = null, int prefetchCount = 0);

Upvotes: 1

Related Questions