You are developing a C# application that must send a batch of telemetry messages to an Azure Service Bus topic. You are using the Azure.Messaging.ServiceBus SDK. To ensure efficient network usage, you decide to send the messages in a single batch. Move the steps required to initialize the client, construct the batch, send the messages, and clean up resources into the correct chronological order.
- 1Create a ServiceBusClient instance using the connection string.
- 2Create a ServiceBusSender instance for the target topic using the client.
- 3Create a ServiceBusMessageBatch instance by calling CreateMessageBatchAsync on the sender.
- 4Call TryAddMessage on the batch instance to add ServiceBusMessage objects.
- 5Send the populated batch using SendMessagesAsync on the sender.
- 6Asynchronously dispose of the client and sender to close connections.
Answer
The correct sequence is to first initialize the ServiceBusClient, then create the ServiceBusSender, followed by calling CreateMessageBatchAsync to prepare the batch. Next, add the messages using TryAddMessage, send the batch using SendMessagesAsync, and finally dispose of the sender and client.
The correct workflow requires establishing the client connection first, obtaining a sender, initializing a size-bounded batch, adding messages safely to that batch, calling the asynchronous send method, and then cleaning up the connection resources.
Step-by-Step Solution
Key Concept
Message batching using the Azure.Messaging.ServiceBus C# SDK
Estimated Time:1m 30s