Azure.Messaging.EventHubs
5.8.0
Prefix Reserved
See the version list below for details.
dotnet add package Azure.Messaging.EventHubs --version 5.8.0
NuGet\Install-Package Azure.Messaging.EventHubs -Version 5.8.0
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.8.0" />
paket add Azure.Messaging.EventHubs --version 5.8.0
#r "nuget: Azure.Messaging.EventHubs, 5.8.0"
// Install Azure.Messaging.EventHubs as a Cake Addin #addin nuget:?package=Azure.Messaging.EventHubs&version=5.8.0 // Install Azure.Messaging.EventHubs as a Cake Tool #tool nuget:?package=Azure.Messaging.EventHubs&version=5.8.0
Azure Event Hubs client library for .NET
Azure Event Hubs is a highly scalable publish-subscribe service that can ingest millions of events per second and stream them to multiple consumers. This lets you process and analyze the massive amounts of data produced by your connected devices and applications. Once Event Hubs has collected the data, you can retrieve, transform, and store it by using any real-time analytics provider or with batching/storage adapters. If you would like to know more about Azure Event Hubs, you may wish to review: What is Event Hubs.
The Azure Event Hubs client library allows for publishing and consuming of Azure Event Hubs events and may be used to:
Emit telemetry about your application for business intelligence and diagnostic purposes.
Publish facts about the state of your application which interested parties may observe and use as a trigger for taking action.
Observe interesting operations and interactions happening within your business or other ecosystem, allowing loosely coupled systems to interact without the need to bind them together.
Receive events from one or more publishers, transform them to better meet the needs of your ecosystem, then publish the transformed events to a new stream for consumers to observe.
Source code | Package (NuGet) | API reference documentation | Product documentation | Migration guide | Troubleshooting guide
Getting started
Prerequisites
Azure Subscription: To use Azure services, including Azure Event Hubs, you'll need a subscription. If you do not have an existing Azure account, you may sign up for a free trial or use your Visual Studio Subscription benefits when you create an account.
Event Hubs namespace with an Event Hub: To interact with Azure Event Hubs, you'll also need to have a namespace and Event Hub available. If you are not familiar with creating Azure resources, you may wish to follow the step-by-step guide for creating an Event Hub using the Azure portal. There, you can also find detailed instructions for using the Azure CLI, Azure PowerShell, or Azure Resource Manager (ARM) templates to create an Event Hub.
C# 8.0: The Azure Event Hubs client library makes use of new features that were introduced in C# 8.0. In order to take advantage of the C# 8.0 syntax, it is recommended that you compile using the .NET Core SDK 3.0 or higher with a language version of
latest
.Visual Studio users wishing to take full advantage of the C# 8.0 syntax will need to use Visual Studio 2019 or later. Visual Studio 2019, including the free Community edition, can be downloaded here. Users of Visual Studio 2017 can take advantage of the C# 8 syntax by making use of the Microsoft.Net.Compilers NuGet package and setting the language version, though the editing experience may not be ideal.
You can still use the library with previous C# language versions, but will need to manage asynchronous enumerable and asynchronous disposable members manually rather than benefiting from the new syntax. You may still target any framework version supported by your .NET Core SDK, including earlier versions of .NET Core or the .NET framework. For more information, see: how to specify target frameworks.
Important Note: In order to build or run the examples and the samples without modification, use of C# 11.0 is necessary. You can still run the samples if you decide to tweak them for other language versions. An example of doing so is available in the sample: Earlier Language Versions.
To quickly create a basic set of Event Hubs resources in Azure and to receive a connection string for them, you can deploy our sample template by clicking:
Install the package
Install the Azure Event Hubs client library for .NET with NuGet:
dotnet add package Azure.Messaging.EventHubs
Authenticate the client
For the Event Hubs client library to interact with an Event Hub, it will need to understand how to connect and authorize with it. The easiest means for doing so is to use a connection string, which is created automatically when creating an Event Hubs namespace. If you aren't familiar with using connection strings with Event Hubs, you may wish to follow the step-by-step guide to get an Event Hubs connection string.
Key concepts
An Event Hub client is the primary interface for developers interacting with the Event Hubs client library. There are several different Event Hub clients, each dedicated to a specific use of Event Hubs, such as publishing or consuming events.
An Event Hub producer is a type of client that serves as a source of telemetry data, diagnostics information, usage logs, or other log data, as part of an embedded device solution, a mobile device application, a game title running on a console or other device, some client or server based business solution, or a web site.
An Event Hub consumer is a type of client which reads information from the Event Hub and allows processing of it. Processing may involve aggregation, complex computation and filtering. Processing may also involve distribution or storage of the information in a raw or transformed fashion. Event Hub consumers are often robust and high-scale platform infrastructure parts with built-in analytics capabilities, like Azure Stream Analytics, Apache Spark, or Apache Storm.
A partition is an ordered sequence of events that is held in an Event Hub. Partitions are a means of data organization associated with the parallelism required by event consumers. Azure Event Hubs provides message streaming through a partitioned consumer pattern in which each consumer only reads a specific subset, or partition, of the message stream. As newer events arrive, they are added to the end of this sequence. The number of partitions is specified at the time an Event Hub is created and cannot be changed.
A consumer group is a view of an entire Event Hub. Consumer groups enable multiple consuming applications to each have a separate view of the event stream, and to read the stream independently at their own pace and from their own position. There can be at most 5 concurrent readers on a partition per consumer group; however it is recommended that there is only one active consumer for a given partition and consumer group pairing. Each active reader receives all of the events from its partition; if there are multiple readers on the same partition, then they will receive duplicate events.
For more concepts and deeper discussion, see: Event Hubs Features.
Client lifetime
Each of the Event Hubs client types is safe to cache and use as a singleton for the lifetime of the application, which is best practice when events are being published or read regularly. The clients are responsible for efficient management of network, CPU, and memory use, working to keep usage low during periods of inactivity. Calling either CloseAsync
or DisposeAsync
on a client is required to ensure that network resources and other unmanaged objects are properly cleaned up.
Thread safety
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
The data model types, such as EventData
and EventDataBatch
are not thread-safe. They should not be shared across threads nor used concurrently with client methods.
Additional concepts
Client options | Handling failures | Diagnostics | Mocking
Examples
Inspect an Event Hub
Many Event Hub operations take place within the scope of a specific partition. Because partitions are owned by the Event Hub, their names are assigned at the time of creation. To understand what partitions are available, you query the Event Hub using one of the Event Hub clients. For illustration, the EventHubProducerClient
is demonstrated in these examples, but the concept and form are common across clients.
var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";
await using (var producer = new EventHubProducerClient(connectionString, eventHubName))
{
string[] partitionIds = await producer.GetPartitionIdsAsync();
}
Publish events to an Event Hub
In order to publish events, you'll need to create an EventHubProducerClient
. Producers publish events in batches and may request a specific partition, or allow the Event Hubs service to decide which partition events should be published to. It is recommended to use automatic routing when the publishing of events needs to be highly available or when event data should be distributed evenly among the partitions. Our example will take advantage of automatic routing.
var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";
await using (var producer = new EventHubProducerClient(connectionString, eventHubName))
{
using EventDataBatch eventBatch = await producer.CreateBatchAsync();
eventBatch.TryAdd(new EventData(new BinaryData("First")));
eventBatch.TryAdd(new EventData(new BinaryData("Second")));
await producer.SendAsync(eventBatch);
}
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. In our example, we will focus on reading all events that have been published to the Event Hub using an iterator.
Note: It is important to note that this approach to consuming is intended to improve the experience of exploring the Event Hubs client library and prototyping. It is recommended that it not be used in production scenarios. For production use, we recommend using the Event Processor Client, as it provides a more robust and performant experience.
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.
}
}
Read events from an Event Hub partition
In order to read events for an Event Hub partition, 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. To read from a specific partition, the consumer will also need to specify where in the event stream to begin receiving events; in our example, we will focus on reading all published events for the first partition of the Event Hub.
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))
{
EventPosition startingPosition = EventPosition.Earliest;
string partitionId = (await consumer.GetPartitionIdsAsync()).First();
using var cancellationSource = new CancellationTokenSource();
cancellationSource.CancelAfter(TimeSpan.FromSeconds(45));
await foreach (PartitionEvent receivedEvent in consumer.ReadEventsFromPartitionAsync(partitionId, startingPosition, cancellationSource.Token))
{
// At this point, the loop will wait for events to be available in the partition. 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.
}
}
Process events using an Event Processor client
For the majority of production scenarios, it is recommended that the Event Processor Client be used for reading and processing events. The processor is intended to provide a robust experience for processing events across all partitions of an Event Hub in a performant and fault tolerant manner while providing a means to persist its state. Event Processor clients are also capable of working cooperatively within the context of a consumer group for a given Event Hub, where they will automatically manage distribution and balancing of work as instances become available or unavailable for the group.
Since the EventProcessorClient
has a dependency on Azure Storage blobs for persistence of its state, you'll need to provide a BlobContainerClient
for the processor, which has been configured for the storage account and container that should be used.
var cancellationSource = new CancellationTokenSource();
cancellationSource.CancelAfter(TimeSpan.FromSeconds(45));
var storageConnectionString = "<< CONNECTION STRING FOR THE STORAGE ACCOUNT >>";
var blobContainerName = "<< NAME OF THE BLOB CONTAINER >>";
var eventHubsConnectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";
var consumerGroup = "<< NAME OF THE EVENT HUB CONSUMER GROUP >>";
Task processEventHandler(ProcessEventArgs eventArgs) => Task.CompletedTask;
Task processErrorHandler(ProcessErrorEventArgs eventArgs) => Task.CompletedTask;
var storageClient = new BlobContainerClient(storageConnectionString, blobContainerName);
var processor = new EventProcessorClient(storageClient, consumerGroup, eventHubsConnectionString, eventHubName);
processor.ProcessEventAsync += processEventHandler;
processor.ProcessErrorAsync += processErrorHandler;
await processor.StartProcessingAsync();
try
{
// The processor performs its work in the background; block until cancellation
// to allow processing to take place.
await Task.Delay(Timeout.Infinite, cancellationSource.Token);
}
catch (TaskCanceledException)
{
// This is expected when the delay is canceled.
}
try
{
await processor.StopProcessingAsync();
}
finally
{
// To prevent leaks, the handlers should be removed when processing is complete.
processor.ProcessEventAsync -= processEventHandler;
processor.ProcessErrorAsync -= processErrorHandler;
}
More details can be found in the Event Processor Client README and the accompanying samples.
Using an Active Directory principal with the Event Hub clients
The Azure Identity library provides Azure Active Directory authentication support which can be used for the Azure client libraries, including Event Hubs.
To make use of an Active Directory principal, one of the available credentials from the Azure.Identity
library is specified when creating the Event Hubs client. In addition, the fully qualified Event Hubs namespace and the name of desired Event Hub are supplied in lieu of the Event Hubs connection string. For illustration, the EventHubProducerClient
is demonstrated in these examples, but the concept and form are common across clients.
var fullyQualifiedNamespace = "<< FULLY-QUALIFIED EVENT HUBS NAMESPACE (like something.servicebus.windows.net) >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";
var credential = new DefaultAzureCredential();
await using (var producer = new EventHubProducerClient(fullyQualifiedNamespace, eventHubName, credential))
{
using EventDataBatch eventBatch = await producer.CreateBatchAsync();
eventBatch.TryAdd(new EventData(new BinaryData("First")));
eventBatch.TryAdd(new EventData(new BinaryData("Second")));
await producer.SendAsync(eventBatch);
}
When using Azure Active Directory, your principal must be assigned a role which allows access to Event Hubs, such as the Azure Event Hubs Data Owner
role. For more information about using Azure Active Directory authorization with Event Hubs, please refer to the associated documentation.
Troubleshooting
For detailed troubleshooting information, please refer to the Event Hubs Troubleshooting Guide.
Logging and diagnostics
The Event Hubs client library is fully instrumented for logging information at various levels of detail using the .NET EventSource
to emit information. Logging is performed for each operation and follows the pattern of marking the starting point of the operation, it's completion, and any exceptions encountered. Additional information that may offer insight is also logged in the context of the associated operation.
The Event Hubs client logs are available to any EventListener
by opting into the source named "Azure-Messaging-EventHubs" or opting into all sources that have the trait "AzureEventSource". To make capturing logs from the Azure client libraries easier, the Azure.Core
library used by Event Hubs offers an AzureEventSourceListener
. More information can be found in Capturing Event Hubs logs using the AzureEventSourceListener.
The Event Hubs client library is also instrumented for distributed tracing using Application Insights or OpenTelemetry. More information can be found in the Azure.Core Diagnostics sample.
Next steps
Beyond the introductory scenarios discussed, the Azure Event Hubs client library offers support for additional scenarios to help take advantage of the full feature set of the Azure Event Hubs service. In order to help explore some of these scenarios, the Event Hubs client library offers a project of samples to serve as an illustration for common scenarios. Please see the samples README for details.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Please see our contributing guide for more information.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Azure.Core (>= 1.29.0)
- Azure.Core.Amqp (>= 1.3.0)
- Microsoft.Azure.Amqp (>= 2.6.1)
- Microsoft.Bcl.AsyncInterfaces (>= 1.1.1)
- System.Diagnostics.DiagnosticSource (>= 4.6.0)
- System.Memory.Data (>= 1.0.2)
- System.Reflection.TypeExtensions (>= 4.7.0)
- System.Threading.Channels (>= 4.7.1)
- System.Threading.Tasks.Extensions (>= 4.5.4)
NuGet packages (76)
Showing the top 5 NuGet packages that depend on Azure.Messaging.EventHubs:
Package | Downloads |
---|---|
Microsoft.Azure.WebJobs.Extensions.EventHubs
Microsoft Azure WebJobs SDK EventHubs Extension |
|
Azure.Messaging.EventHubs.Processor
Azure Event Hubs is a highly scalable publish-subscribe service that can ingest millions of events per second and stream them to multiple consumers. This library extends its Event Processor with durable storage for checkpoint information using Azure Blob storage. For more information about Event Hubs, see https://azure.microsoft.com/en-us/services/event-hubs/ |
|
Microsoft.Azure.Functions.Worker.Extensions.EventHubs
Azure Event Hubs extensions for .NET isolated functions |
|
Serilog.Sinks.AzureEventHub
Write Serilog events to Azure Event Hub |
|
Microsoft.Diagnostics.EventFlow.Outputs.EventHub
Provides an output implementation that sends diagnostics data to Azure Event Hubs. |
GitHub repositories (22)
Showing the top 5 popular GitHub repositories that depend on Azure.Messaging.EventHubs:
Repository | Stars |
---|---|
dotnet/orleans
Cloud Native application framework for .NET
|
|
Azure/azure-sdk-for-net
This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
|
|
Xabaril/AspNetCore.Diagnostics.HealthChecks
Enterprise HealthChecks for ASP.NET Core Diagnostics Package
|
|
dotnet/samples
Sample code referenced by the .NET documentation
|
|
phongnguyend/Practical.CleanArchitecture
Full-stack .Net 8 Clean Architecture (Microservices, Modular Monolith, Monolith), Blazor, Angular 18, React 18, Vue 3, BFF with YARP, Domain-Driven Design, CQRS, SOLID, Asp.Net Core Identity Custom Storage, OpenID Connect, Entity Framework Core, OpenTelemetry, SignalR, Hosted Services, Health Checks, Rate Limiting, Cloud Services (Azure, AWS, GCP).
|
Version | Downloads | Last updated |
---|---|---|
5.12.0-beta.1 | 8,985 | 5/17/2024 |
5.11.5 | 1,127,987 | 7/31/2024 |
5.11.4 | 283,970 | 7/18/2024 |
5.11.3 | 1,114,456 | 5/15/2024 |
5.11.2 | 987,403 | 4/10/2024 |
5.11.1 | 863,730 | 3/5/2024 |
5.11.0 | 326,903 | 2/13/2024 |
5.10.0 | 2,300,857 | 11/8/2023 |
5.9.3 | 1,637,877 | 9/12/2023 |
5.9.2 | 3,720,180 | 6/6/2023 |
5.9.1 | 541,352 | 5/9/2023 |
5.9.0 | 897,763 | 4/11/2023 |
5.8.1 | 495,949 | 3/10/2023 |
5.8.0 | 50,611 | 3/7/2023 |
5.7.5 | 2,510,950 | 11/22/2022 |
5.7.4 | 3,751,910 | 11/8/2022 |
5.7.3 | 811,424 | 10/11/2022 |
5.7.2 | 2,303,902 | 8/9/2022 |
5.7.1 | 2,381,343 | 7/7/2022 |
5.7.0 | 2,131,408 | 5/10/2022 |
5.7.0-beta.5 | 11,559 | 4/5/2022 |
5.7.0-beta.4 | 9,131 | 3/11/2022 |
5.7.0-beta.3 | 14,090 | 2/9/2022 |
5.7.0-beta.2 | 11,373 | 1/13/2022 |
5.7.0-beta.1 | 13,257 | 11/9/2021 |
5.6.2 | 7,675,509 | 10/5/2021 |
5.6.1 | 947,730 | 9/8/2021 |
5.6.0 | 400,059 | 8/10/2021 |
5.5.0 | 737,657 | 7/7/2021 |
5.5.0-beta.1 | 1,361 | 6/8/2021 |
5.4.1 | 2,004,916 | 5/11/2021 |
5.4.0 | 565,863 | 4/5/2021 |
5.4.0-beta.1 | 1,251 | 3/17/2021 |
5.3.1 | 1,935,071 | 3/9/2021 |
5.3.0 | 375,195 | 2/9/2021 |
5.3.0-beta.4 | 239,192 | 11/10/2020 |
5.3.0-beta.3 | 7,346 | 9/30/2020 |
5.3.0-beta.1 | 2,483 | 9/15/2020 |
5.2.0 | 2,165,810 | 9/8/2020 |
5.2.0-preview.3 | 5,252 | 8/18/2020 |
5.2.0-preview.1 | 6,031 | 7/6/2020 |
5.1.0 | 821,501 | 5/5/2020 |
5.1.0-preview.1 | 1,280 | 4/6/2020 |
5.0.1 | 362,172 | 1/29/2020 |
5.0.0-preview.6 | 21,531 | 12/3/2019 |
5.0.0-preview.5 | 4,565 | 11/1/2019 |
5.0.0-preview.4 | 2,979 | 10/8/2019 |
5.0.0-preview.3 | 1,177 | 9/10/2019 |
5.0.0-preview.2 | 1,814 | 8/6/2019 |
5.0.0-preview.1 | 2,149 | 7/1/2019 |