Soru

Zorluk: OrtaImplement Azure Service Bus Solutions

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?

  1. A
    Create a `ServiceBusReceiver` with the `ReceiveMode` set to `ReceiveAndDelete`, process the message, and call `CompleteMessageAsync` on the receiver after successful processing.
  2. Create a `ServiceBusReceiver` using default options, process the message, and call `CompleteMessageAsync` on the receiver after successful processing.Cevap
  3. C
    Create 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.
  4. D
    Create 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.

Cevap

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.

Adım Adım Çözüm

1
Identify the appropriate Azure Service Bus receive mode for crash-safety requirements.
Determine that PeekLock mode must be used rather than ReceiveAndDelete, because PeekLock guarantees that messages are not lost if the receiver crashes during processing.
ReceiveAndDelete removes the message immediately upon receipt, causing message loss in the event of a crash, which violates the requirement.
2
Select the default receive mode or configure it explicitly as PeekLock.
A ServiceBusReceiver created with default options uses PeekLock mode by default.
Default configuration reduces code complexity while meeting the safety requirements.
3
Handle message settlement in the client code.
Call CompleteMessageAsync on the receiver instance once the processing succeeds.
With ServiceBusReceiver, PeekLock messages are not automatically settled; they must be explicitly completed to be removed from the queue.

Anahtar Kavram

Azure Service Bus Receive Modes and Message Settlement
Tahmini Süre:1m 30s
Bu soruyu puanla