Question

Difficulty: MediumImplement Azure Service Bus Solutions

You are developing a C# background service that processes smart meter telemetry data from an Azure Service Bus queue named 'meter-telemetry'. If the processing of a telemetry message fails due to an external API outage, the message must not be lost and should remain in the queue for another attempt. You need to initialize the receiver and handle the message processing in a way that guarantees at-least-once delivery. Which code segment should you use?

  1. var options = new ServiceBusReceiverOptions { ReceiveMode = ServiceBusReceiveMode.PeekLock };
    var receiver = client.CreateReceiver("meter-telemetry", options);
    var message = await receiver.ReceiveMessageAsync();
    // Process message...
    await receiver.CompleteMessageAsync(message);
    Answer
  2. B
    var options = new ServiceBusReceiverOptions { ReceiveMode = ServiceBusReceiveMode.ReceiveAndDelete };
    var receiver = client.CreateReceiver("meter-telemetry", options);
    var message = await receiver.ReceiveMessageAsync();
    // Process message...
    await receiver.CompleteMessageAsync(message);
  3. C
    var options = new ServiceBusReceiverOptions { ReceiveMode = ServiceBusReceiveMode.PeekLock };
    var receiver = client.CreateReceiver("meter-telemetry", options);
    var message = await receiver.ReceiveMessageAsync();
    // Process message...
    // Allow the lock duration to expire naturally
  4. D
    var options = new ServiceBusReceiverOptions { ReceiveMode = ServiceBusReceiveMode.ReceiveAndDelete };
    var receiver = client.CreateReceiver("meter-telemetry", options);
    var message = await receiver.ReceiveMessageAsync();
    // Process message...

Answer

Initialize the receiver using ServiceBusReceiveMode.PeekLock and explicitly call CompleteMessageAsync after processing is complete.
The correct approach uses PeekLock mode. This ensures that the message is only deleted from the queue when CompleteMessageAsync is explicitly called after successful processing. If the background service encounters a transient failure or crashes before completion, the lock expires and the message is released back to the queue for retry, ensuring at-least-once delivery.

Step-by-Step Solution

1
Set the ServiceBusReceiverOptions ReceiveMode property.
Use ServiceBusReceiveMode.PeekLock to ensure that messages are locked rather than instantly deleted upon retrieval.
This guarantees that if the receiver fails mid-process, the message lock will expire and the message will become available again on the queue.
2
Invoke receiver.ReceiveMessageAsync() and perform the required business logic.
Receive the message payload and process the meter telemetry data.
During this phase, the message remains hidden from other consumers for the duration of the lock.
3
Invoke receiver.CompleteMessageAsync(message) upon successful execution.
The message is permanently deleted from the queue.
Explicit completion is required in PeekLock mode to confirm successful processing and remove the message.

Key Concept

Azure Service Bus Receive Modes
Estimated Time:1m 30s
Rate this question