Question

Difficulty: HardImplement Azure Service Bus Solutions

An enterprise inventory application uses Azure Service Bus to coordinate message routing. A developer is implementing a transaction-based message processing flow in C# using the Azure.Messaging.ServiceBus SDK. The application must receive a message from an input queue named orders-input, send a related message to an output queue named orders-output (within the same namespace), and then complete the original message. The entire sequence must occur inside a single atomic transaction.

The developer writes the following implementation:

csharp
using System.Transactions;
using Azure.Messaging.ServiceBus;

// ... client initialization ...

var options = new ServiceBusReceiverOptions
{
ReceiveMode = // [Configuration here]
};
ServiceBusReceiver receiver = client.CreateReceiver("orders-input", options);
ServiceBusSender sender = client.CreateSender("orders-output");

using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
ServiceBusReceivedMessage message = await receiver.ReceiveMessageAsync();
// processing logic...

var response = new ServiceBusMessage("Order Processed");
await sender.SendMessageAsync(response);
await receiver.CompleteMessageAsync(message);

ts.Complete();
}

Which of the following configurations is required to ensure that if the send or complete operation fails, the transaction rolls back and the original message remains in the input queue?

  1. A
    Configure the `ReceiveMode` to `ServiceBusReceiveMode.ReceiveAndDelete`.
  2. B
    Configure the `ReceiveMode` to `ServiceBusReceiveMode.PeekLock` and connect using a Shared Access Signature (SAS) token restricted to the Manage permission scope.
  3. Configure the `ReceiveMode` to `ServiceBusReceiveMode.PeekLock`.Answer
  4. D
    Configure the `ReceiveMode` to `ServiceBusReceiveMode.PeekLock` and retrieve the connection string from Azure Key Vault using a managed identity that lacks Secret Get access policy permissions.

Answer

The correct configuration is to configure the `ReceiveMode` to `ServiceBusReceiveMode.PeekLock`.
Configuring the receive mode to `PeekLock` ensures that the Service Bus message is not deleted from the queue immediately upon receipt. Instead, the message is locked for a duration, allowing the application to process the payload and send the output message. When enclosed within a `TransactionScope`, the message settlement (complete) is committed atomically with the send operation. If any operation within the scope fails and the transaction rolls back, the lock is released, and the original message remains safely in the input queue.

Step-by-Step Solution

1
Understand the transactional requirement.
The operation must atomically receive a message, send a message, and complete the original message.
Ensures that if any part of the process fails, the original message is not lost.
2
Analyze the Service Bus receive modes.
Identify that `PeekLock` keeps the message on the queue with a lock, while `ReceiveAndDelete` deletes it immediately.
Only `PeekLock` supports explicit settlement (like completion) that can be committed or rolled back within a transaction.
3
Verify security and credential requirements.
Ensure that the client has Send/Listen permissions and Key Vault access is authorized.
Any misconfiguration in permissions or key retrieval would prevent client initialization and block processing.

Key Concept

The core concept being tested is implementing transactional message processing in Azure Service Bus by selecting the correct receive mode (`PeekLock`) to enable atomic send and complete operations.
Rate this question