You are developing a .NET background worker application that processes messages from an Azure Service Bus queue named sensor-telemetry using the Azure.Messaging.ServiceBus SDK. The messages contain environmental data that must be successfully saved to an external SQL database. If the database is offline, the message must not be lost and must remain in the queue to be retried later. Which approach should you use to receive and process the messages?
- Receive messages using the default PeekLock mode, process the telemetry data, and then call CompleteMessageAsync on the receiver.Cevap
- BConfigure the ServiceBusReceiver to use ReceiveAndDelete mode to retrieve and process the telemetry messages.
- CUse the PeekMessageAsync method on the receiver to retrieve the message, process it, and then call CompleteMessageAsync to remove it.
- DUse a system-assigned managed identity authorized with only Key Vault Secrets User permissions to manage queue message deletion.
Cevap
Receive messages using the default PeekLock mode, process the telemetry data, and then call CompleteMessageAsync on the receiver.
Receiving messages using the default PeekLock mode ensures that the message is locked on the queue and invisible to other receivers during processing. Calling CompleteMessageAsync only after the database write succeeds guarantees that the message is safely removed from the queue only when processing is fully successful. If the database is offline, the operation fails before completion, the lock eventually expires, and the message returns to the queue to be processed again.
Adım Adım Çözüm
Anahtar Kavram
Azure Service Bus Receive Modes and SDK message lifecycle management