Skip to content

Commit ec0324e

Browse files
committed
added sample code
1 parent cac048b commit ec0324e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

articles/event-hubs/event-hubs-availability-and-consistency.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,42 @@ For use cases that require the maximum up time, this model is preferred.
4848
## Consistency
4949
In some scenarios, the ordering of events can be important. For example, you may want your back-end system to process an update command before a delete command. In this instance, you can either set the partition key on an event, or use a `PartitionSender` object (if you are using the old Microsoft.Azure.Messaging library) to only send events to a certain partition. Doing so ensures that when these events are read from the partition, they are read in order. If you are using the **Azure.Messaging.EventHubs** library and for more information, see [Migrating code from PartitionSender to EventHubProducerClient for publishing events to a partition](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/eventhub/Azure.Messaging.EventHubs/MigrationGuide.md#migrating-code-from-partitionsender-to-eventhubproducerclient-for-publishing-events-to-a-partition).
5050

51+
#### [Azure.Messaging.EventHubs (5.0.0 or later)](#tab/latest)
52+
var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
53+
var eventHubName = "<< NAME OF THE EVENT HUB >>";
54+
55+
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
56+
{
57+
var batchOptions = new CreateBatchOptions() { PartitionId = "my-partition-id" };
58+
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(batchOptions);
59+
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("First")));
60+
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Second")));
61+
62+
await producerClient.SendAsync(eventBatch);
63+
}
64+
#### [Microsoft.Azure.EventHubs (4.1.0 or earlier)](#tab/old)
65+
var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
66+
var eventHubName = "<< NAME OF THE EVENT HUB >>";
67+
68+
var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString){ EntityPath = eventHubName };
69+
var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
70+
PartitionSender partitionSender = eventHubClient.CreatePartitionSender("my-partition-id");
71+
try
72+
{
73+
EventDataBatch eventBatch = partitionSender.CreateBatch();
74+
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("First")));
75+
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Second")));
76+
77+
await partitionSender.SendAsync(eventBatch);
78+
}
79+
finally
80+
{
81+
await partitionSender.CloseAsync();
82+
await eventHubClient.CloseAsync();
83+
}
84+
85+
---
86+
5187
With this configuration, keep in mind that if the particular partition to which you are sending is unavailable, you will receive an error response. As a point of comparison, if you do not have an affinity to a single partition, the Event Hubs service sends your event to the next available partition.
5288

5389
One possible solution to ensure ordering, while also maximizing up time, would be to aggregate events as part of your event processing application. The easiest way to accomplish this is to stamp your event with a custom sequence number property. The following code shows an example:

0 commit comments

Comments
 (0)