Question

Difficulty: EasyImplement Azure Event Hubs Solutions

You are developing a C# application using the .NET SDK Azure.Messaging.EventHubs to send telemetry data to an Azure Event Hub. You need to write the code to send a batch of events efficiently and reliably. Which two actions must you perform to create and publish the event batch using the SDK? (Select TWO).

  1. Create an EventDataBatch object by calling CreateBatchAsync on the EventHubProducerClient instance.Answer
  2. Add events to the batch using the TryAdd method, and then transmit the batch by calling SendAsync.Answer
  3. C
    Call the AcquireLeaseAsync method on a BlobClient before sending the batch to prevent concurrent write conflicts on the Event Hub partitions.
  4. D
    Initialize the EventHubProducerClient specifically with a system-assigned managed identity, as user-assigned managed identities are not supported for the Azure Event Hubs Data Sender role.

Answer

To publish a batch of events to an Azure Event Hub using the modern .NET SDK, you must create an EventDataBatch instance by calling CreateBatchAsync on the EventHubProducerClient, append events to it using the TryAdd method to ensure they do not exceed size constraints, and then send the completed batch using SendAsync.
Publishing events in batches using the .NET SDK requires calling CreateBatchAsync on an EventHubProducerClient to manage the size constraints. Individual events are added via TryAdd, and the completed batch is sent to the Event Hub using SendAsync.

Step-by-Step Solution

1
Instantiate an EventHubProducerClient and call CreateBatchAsync.
An EventDataBatch object is created, which is pre-configured with the maximum size allowed for a single transmission based on the Event Hub service limits.
This guarantees that the payload being assembled will not exceed the maximum allowed message size.
2
Call TryAdd on the EventDataBatch object for each EventData payload to be sent.
Events are successfully added to the batch if they fit within the size limit. The method returns false if the event is too large to fit in the current batch.
This provides client-side validation of message size limits, preventing runtime failures during transmission.
3
Pass the EventDataBatch to the SendAsync method of the EventHubProducerClient.
The batch of events is sent to the Azure Event Hub over the wire.
This sends all buffered events in a single network transaction, maximizing performance and efficiency.

Key Concept

Batch publishing pattern with EventHubProducerClient in the Azure.Messaging.EventHubs SDK
Rate this question