Quantcast
Channel: Service Bus forum
Viewing all articles
Browse latest Browse all 1916

Getting an exception: Message handler encountered an exception Microsoft.Azure.ServiceBus.MessageLockLostException: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. while removing message.

$
0
0

HI ,

I am getting below error while removing message from Queue.

Message handler encountered an exception Microsoft.Azure.ServiceBus.MessageLockLostException: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue.

I am receiving message then performing log running task then I am trying to delete/remove message from queue.

here is code that I have used.

 private async Task ProcessAgentServiceQueueMessagesAsync(Message message, CancellationToken token)
        {
            try
            {

                    //Create a CTS to launch a task in charge of renewing the message lock                     

                    var messageRenewCancellationTokenSource = new CancellationTokenSource();

                    var brokeredMessageRenew = Task.Factory.StartNew(() =>
                    {
                        while (!messageRenewCancellationTokenSource.Token.IsCancellationRequested)
                        {
                            //Based on LockedUntilUtc property to determine if the lock expires soon
                            if (DateTime.UtcNow > message.SystemProperties.LockedUntilUtc.AddSeconds(-10))
                            {
                                // If so, we repeat the message
                                messageReceiver.RenewLockAsync(message.SystemProperties.LockToken);
                            }

                            Thread.Sleep(500);
                        }
                    }, messageRenewCancellationTokenSource.Token);
                    await Task.Run(() => LongRunningfunction());
                   await messageReceiver.CompleteAsync(message.SystemProperties.LockToken);

         }

          catch(Exception ex)

         {

         }

        finally

       {

               messageRenewCancellationTokenSource.Cancel();

       }

}

Please let me know, what is wrong in this code. I am renewing lock before token get expire still I am getting exception and message is remaining in queue.


Viewing all articles
Browse latest Browse all 1916

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>