How-To read events from an Event Hub
In order to read events from an Event Hub, you'll need to create an EventHubConsumerClient
for a given consumer group. When an Event Hub is created, it provides a default consumer group that can be used to get started with exploring Event Hubs : azure-sdk-for-net/README.md at main · Azure/azure-sdk-for-net · GitHub
var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";
string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;
await using (var consumer = new EventHubConsumerClient(consumerGroup,
connectionString, eventHubName))
{
using var cancellationSource = new CancellationTokenSource();
cancellationSource.CancelAfter(TimeSpan.FromSeconds(45));
await foreach (PartitionEvent receivedEvent in
consumer.ReadEventsAsync(cancellationSource.Token))
{
// At this point, the loop will wait for events to be available in the Event Hub.
// When an event
// is available, the loop will iterate with the event that was received.
// Because we did not
// specify a maximum wait time, the loop will wait forever unless cancellation
// is requested using the cancellation token.
}
}