Soru

Zorluk: OrtaImplement Azure Service Bus Solutions

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.

  1. 1Create a ServiceBusClient instance using the connection string.
  2. 2Create a ServiceBusSender instance for the target topic using the client.
  3. 3Create a ServiceBusMessageBatch instance by calling CreateMessageBatchAsync on the sender.
  4. 4Call TryAddMessage on the batch instance to add ServiceBusMessage objects.
  5. 5Send the populated batch using SendMessagesAsync on the sender.
  6. 6Asynchronously dispose of the client and sender to close connections.

Cevap

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.

Adım Adım Çözüm

1
Instantiate ServiceBusClient
A ServiceBusClient object is initialized.
The client is required to connect to the namespace.
2
Create ServiceBusSender
A ServiceBusSender object is created.
The sender is required to send messages to the topic.
3
Initialize ServiceBusMessageBatch
A ServiceBusMessageBatch object is created.
The batch ensures the overall message sizes do not exceed service limits.
4
Add messages via TryAddMessage
Messages are added to the batch.
This safely checks size limits before sending.
5
Call SendMessagesAsync
The batch is sent.
Transmits the batch of messages in one network operation.
6
Dispose of client and sender
Resources are freed.
Prevents connection leaks and cleans up AMQP channels.

Anahtar Kavram

Message batching using the Azure.Messaging.ServiceBus C# SDK
Tahmini Süre:1m 30s
Bu soruyu puanla