Search code examples
azureservicebus

How to removed message from azure service bus queue if it causes an unhandled exception?


I am building an azure application that uses the Azure Service Bus Queue and I'm running into the following scenario :

My worker role receives a message from the service bus queue and starts to process it. During this process, an unhandled exception occurs (can't be caught). This causes the worker role instance to recycle. Since the instance recycled, my message was never completed and is still sitting on the queue. Once my instance recycles and starts, it grabs the first message on the queue which is what caused the unhandled exception.

Is there any way to handle this scenario and remove the message from the queue?


Solution

  • Is there any way to handle this scenario and remove the message from the queue?

    As Gaurave Mantri mentioned that you could set the Max delivery count to the vaule that you want. The default value is 10. For more detail info you could refer to Exceeding MaxDeliveryCount. If the retry times up to Max delivery then the queue message will move to dead letter queue automatically.

    Queues and subscriptions each have a QueueDescription.MaxDeliveryCount and SubscriptionDescription.MaxDeliveryCount property respectively; the default value is 10. Whenever a message has been delivered under a lock (ReceiveMode.PeekLock), but has been either explicitly abandoned or the lock has expired, the message BrokeredMessage.DeliveryCount is incremented. This behavior cannot be disabled.