Question

Difficulty: MediumImplement Azure Service Bus Solutions

You are developing a .NET background service using the Azure.Messaging.ServiceBus SDK to process payroll update messages from an Azure Service Bus queue named payroll-queue. The queue has sessions enabled.

The service must meet the following requirements:
- Process messages in the exact order they were sent within each session.
- Ensure that no message is lost if the background service encounters an unhandled exception during processing.
- Follow the principle of least privilege for security and access control.

Which two actions should you perform to implement these requirements? (Select two.)

  1. Call client.AcceptNextSessionAsync("payroll-queue") to obtain a session receiver.Answer
  2. Call receiver.CompleteMessageAsync(message) after the message is successfully processed.Answer
  3. C
    Configure the receiver to use ServiceBusReceiveMode.ReceiveAndDelete to automatically delete messages upon retrieval.
  4. D
    Authenticate the client using a Shared Access Signature (SAS) token that has the Manage permission on the Service Bus namespace.
  5. E
    Configure the Azure Key Vault access policy to grant the service's identity only the Set permission to retrieve the Service Bus connection string.

Answer

To implement these requirements, you must obtain a session-enabled receiver using the AcceptNextSessionAsync method, and settle each message by calling CompleteMessageAsync after successful processing.
Accepting the next session ensures that messages with the same session ID are processed in sequence by a single receiver instance. Completing the message manually under PeekLock mode guarantees that if an unhandled exception occurs before completion, the lock will release and another instance can retry the message, ensuring zero message loss.

Step-by-Step Solution

1
Establish session-based message retrieval.
Call AcceptNextSessionAsync on the ServiceBusClient.
Because the queue has sessions enabled, standard receivers cannot read from it. A session receiver locks a specific session and ensures messages within that session are received sequentially.
2
Settle messages reliably to prevent data loss.
Process the message using the default PeekLock mode, then call CompleteMessageAsync.
PeekLock locks the message temporarily. If the processing fails or an unhandled exception occurs, the lock expires and the message is returned to the queue. Explicitly completing it deletes it only after successful execution.
3
Apply least privilege authentication.
Ensure the identity or token uses Listen-only permissions, and has Get permission on the Key Vault secrets.
Receiving messages requires only Listen permissions, and reading secrets from Key Vault requires Get permissions, maintaining a secure design.

Key Concept

Azure Service Bus sessions enable FIFO message ordering within a session context, which must be paired with PeekLock receive mode and explicit message settlement to ensure transactional reliability and prevent message loss.
Rate this question