The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue

前端 未结 3 1521
执笔经年
执笔经年 2020-12-08 19:24

I\'m using a Microsoft azure service bus queue to process calculations and my program runs fine for a few hours but then I start to get this exception for every message that

相关标签:
3条回答
  • 2020-12-08 19:46

    I was having a similar issue. Messages were being handled successfully, but when they went to complete, the Service Bus didn't have a valid lock anymore. It turns out my TopicClient.PrefetchCount was too high.

    It appears that the lock begins on all prefetched messages as soon as they are fetched. If your cumulative message processing time surpasses the lock timeout every other prefetched message will fail to complete. It will return to the service bus.

    0 讨论(0)
  • 2020-12-08 19:50

    I spent hours trying understand why I was getting a MessageLockLostException. The reason for me was due to AutoComplete defaulting to true.

    If you're going to call messsage.Complete() (or CompleteAsync()) then you should instantiate an OnMessageOptions object, set AutoComplete to false, and pass it into your OnMessage call.

    var options = new OnMessageOptions();
    options.AutoComplete = false;
    
    client.OnMessage(processCalculations, options);
    
    0 讨论(0)
  • 2020-12-08 19:58

    Instead of renewing the lock manualy, when you create the client subscription, try auto renewing it using the client's OnMessageOptions() like this:

            OnMessageOptions options = new OnMessageOptions();
    
            options.AutoRenewTimeout = TimeSpan.FromMinutes(1);
    
            try
            {
                client = Subscription.CreateClient();
    
                client.OnMessageAsync(MessageReceivedComplete, options);
            }
            catch (Exception ex)
            {
                throw new Exception (ex);
            }
    
    0 讨论(0)
提交回复
热议问题