A logistics company implements a package dispatch system using an Azure Service Bus queue named `dispatch-queue`. You are writing a C# console application using the `Azure.Messaging.ServiceBus` SDK to process dispatch jobs. The application must guarantee that a dispatch job is only removed from the queue after it is successfully processed and saved to the database. If a database timeout or application crash occurs during processing, the message must become available for other receiver instances after the lock expires.
Which approach should you implement in your C# application to meet these requirements?
- ACreate a `ServiceBusReceiver` with the `ReceiveMode` set to `ReceiveAndDelete`, process the message, and call `CompleteMessageAsync` on the receiver after successful processing.
- Create a `ServiceBusReceiver` using default options, process the message, and call `CompleteMessageAsync` on the receiver after successful processing.Answer
- CCreate a `ServiceBusReceiver` using default options, process the message, and rely on the receiver to automatically delete the message from the queue once it has been successfully returned to the client code.
- DCreate a `ServiceBusReceiver` with the `ReceiveMode` set to `ReceiveAndDelete`, process the message, and rely on the queue to automatically return the message to the queue if the receiver connection drops before completion.
Answer
Create a ServiceBusReceiver using default options, process the message, and call CompleteMessageAsync on the receiver after successful processing.
The correct approach uses the default PeekLock mode. When a message is retrieved under PeekLock, it remains in the queue but is locked for other receivers. The receiving client must explicitly call CompleteMessageAsync to delete the message from the queue after successful processing. If the client crashes or fails to process the message before the lock duration expires, the lock is released, and the message becomes visible to other receivers, satisfying the durability requirement.
Step-by-Step Solution
Key Concept
Azure Service Bus Receive Modes and Message Settlement
Estimated Time:1m 30s