Skip to main content
A presentation slide titled "Working with Event Hub .NET SDK." It features a blue-green curved background on the right with an Event Hub-style icon and a small © Copyright KodeKloud note.
This guide demonstrates common patterns for using the Azure Event Hubs .NET SDK: creating producer and consumer clients, inspecting partitions, publishing event batches, and reading events. It includes short focused examples and a complete Program.cs sample you can run locally. Reference: Azure Event Hubs .NET SDK overview and Azure Event Hubs documentation. Quick summary (what you’ll learn)
  • How to instantiate EventHubProducerClient and examine partition IDs.
  • How to create an EventDataBatch, add events, and send them to an Event Hub.
  • How to instantiate EventHubConsumerClient and read events with a cancellation token.
Overview of primary SDK types Instantiating an Event Hub (inspect partitions) To connect and inspect partition information, create an EventHubProducerClient. Retrieving partition IDs with GetPartitionIdsAsync() helps you understand how messages will be distributed across partitions.
Creating and sending a batch of events Batching is efficient and ensures the client respects the service-side size constraints. Create an EventDataBatch with CreateBatchAsync, add events via TryAdd, and then send the batch using SendAsync.
Reading events from an Event Hub Use an EventHubConsumerClient to read from a consumer group. ReadEventsAsync returns a stream of PartitionEvent instances. Use a CancellationTokenSource to limit how long the consumer listens.
A complete example The consolidated Program.cs below demonstrates inspecting partitions, publishing a batch, and reading events. Replace connectionString with the connection string you copy from the Azure portal (either the Event Hubs namespace or hub-level SAS policy). Ensure the connection string you use has send and/or listen permissions as needed.
Make sure the connection string has the appropriate permissions (send and/or listen). Create a SAS policy at the Event Hub level or use the namespace-level RootManageSharedAccessKey if you need all permissions.
Running the example
  • Build and run with dotnet run.
  • The program will:
    1. List the Event Hub partition IDs.
    2. Publish the two example events as a single batch.
    3. Listen for incoming events for up to 45 seconds and print any that arrive.
  • If other producers (for example, an App Service configured to send to this hub) are sending messages, this consumer will receive them while it is listening.
Best practices and tips
  • Scope clients to the operation: create separate producer/consumer instances for distinct operations or lifetimes.
  • Use EventDataBatch to avoid exceeding service-side size limits when sending multiple events.
  • Always use a cancellation token when reading so the consumer doesn’t block indefinitely.
  • For production, consider using retry policies and proper error handling for transient failures.
Further reading and references

Watch Video

Practice Lab