title | description | ms.topic | ms.date | ms.devlang | ms.custom |
---|---|---|---|---|---|
Quickstart: Send or receive events using .NET |
A quickstart that shows you how to create a .NET Core application that sends events to and receive events from Azure Event Hubs. |
quickstart |
03/24/2025 |
csharp |
devx-track-csharp, mode-api, passwordless-dotnet, devx-track-dotnet |
In this quickstart, you learn how to send events to an event hub and then receive those events from the event hub using the Azure.Messaging.EventHubs .NET library.
Note
Quickstarts are for you to quickly ramp up on the service. If you're already familiar with the service, you might want to see .NET samples for Event Hubs in our .NET SDK repository on GitHub: Event Hubs samples on GitHub, Event processor samples on GitHub.
If you're new to Azure Event Hubs, see Event Hubs overview before you go through this quickstart.
To complete this quickstart, you need the following prerequisites:
- Microsoft Azure subscription. To use Azure services, including Azure Event Hubs, you need a subscription. If you don't have an existing Azure account, you can sign up for a free trial.
- Microsoft Visual Studio 2022. The Azure Event Hubs client library makes use of new features that were introduced in C# 8.0. You can still use the library with previous C# language versions, but the new syntax isn't available. To make use of the full syntax, we recommend that you compile with the .NET Core SDK 3.0 or higher and language version set to
latest
. If you're using Visual Studio, versions before Visual Studio 2022 aren't compatible with the tools needed to build C# 8.0 projects. Visual Studio 2022, including the free Community edition, can be downloaded here. - Create an Event Hubs namespace and an event hub. The first step is to use the Azure portal to create an Event Hubs namespace and an event hub in the namespace. Then, obtain the management credentials that your application needs to communicate with the event hub. To create a namespace and an event hub, see Quickstart: Create an event hub using Azure portal.
[!INCLUDE event-hub-passwordless-template-tabbed]
This section shows you how to create a .NET Core console application to send events to the event hub you created.
-
If you have Visual Studio 2022 open already, select File on the menu, select New, and then select Project. Otherwise, launch Visual Studio 2022 and select Create a new project if you see a popup window.
-
On the Create a new project dialog box, do the following steps: If you don't see this dialog box, select File on the menu, select New, and then select Project.
-
Select C# for the programming language.
-
Select Console for the type of the application.
-
Select Console Application from the results list.
-
Then, select Next.
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/new-send-project.png" alt-text="Image showing the New Project dialog box":::
-
-
Enter EventHubsSender for the project name, EventHubsQuickStart for the solution name, and then select Next.
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/project-solution-names.png" alt-text="Image showing the page where you enter solution and project names":::
-
On the Additional information page, select Create.
-
Select Tools > NuGet Package Manager > Package Manager Console from the menu.
-
Run the following commands to install Azure.Messaging.EventHubs and Azure.Identity NuGet packages. Press ENTER to run the second command.
Install-Package Azure.Messaging.EventHubs Install-Package Azure.Identity
-
Select Tools > NuGet Package Manager > Package Manager Console from the menu.
-
Run the following command to install the Azure.Messaging.EventHubs NuGet package:
Install-Package Azure.Messaging.EventHubs
-
Replace the existing code in the
Program.cs
file with the following sample code. Then, replace<EVENT_HUB_NAMESPACE>
and<HUB_NAME>
placeholder values for theEventHubProducerClient
parameters with the names of your Event Hubs namespace and the event hub. For example:"spehubns0309.servicebus.windows.net"
and"spehub"
.Here are the important steps from the code:
- Creates an EventHubProducerClient object using the namespace and the event hub name.
- Invokes the CreateBatchAsync method on the EventHubProducerClient object to create an EventDataBatch object.
- Add events to the batch using the EventDataBatch.TryAdd method.
- Sends the batch of messages to the event hub using the EventHubProducerClient.SendAsync method.
using Azure.Identity; using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Producer; using System.Text; // number of events to be sent to the event hub int numOfEvents = 3; // The Event Hubs client types are 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. // TODO: Replace the <EVENT_HUB_NAMESPACE> and <HUB_NAME> placeholder values EventHubProducerClient producerClient = new EventHubProducerClient( "<EVENT_HUB_NAMESPACE>.servicebus.windows.net", "<HUB_NAME>", new DefaultAzureCredential()); // Create a batch of events using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); for (int i = 1; i <= numOfEvents; i++) { if (!eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes($"Event {i}")))) { // if it is too large for the batch throw new Exception($"Event {i} is too large for the batch and cannot be sent."); } } try { // Use the producer client to send the batch of events to the event hub await producerClient.SendAsync(eventBatch); Console.WriteLine($"A batch of {numOfEvents} events has been published."); Console.ReadLine(); } finally { await producerClient.DisposeAsync(); }
-
Replace the existing code in the
Program.cs
file with the following sample code. Then, replace the<CONNECTION_STRING>
and<HUB_NAME>
placeholder values for theEventHubProducerClient
parameters.Here are the important steps from the code:
- Creates a EventHubProducerClient object using the primary connection string to the namespace and the event hub name.
- Invokes the CreateBatchAsync method on the EventHubProducerClient object to create a EventDataBatch object.
- Add events to the batch using the EventDataBatch.TryAdd method.
- Sends the batch of messages to the event hub using the EventHubProducerClient.SendAsync method.
using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Producer; using System.Text; // number of events to be sent to the event hub int numOfEvents = 3; // The Event Hubs client types are 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. // TODO: Replace the <CONNECTION_STRING> and <HUB_NAME> placeholder values EventHubProducerClient producerClient = new EventHubProducerClient( "<CONNECTION_STRING>", "<HUB_NAME>"); // Create a batch of events using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); for (int i = 1; i <= numOfEvents; i++) { if (!eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes($"Event {i}")))) { // if it is too large for the batch throw new Exception($"Event {i} is too large for the batch and cannot be sent."); } } try { // Use the producer client to send the batch of events to the event hub await producerClient.SendAsync(eventBatch); Console.WriteLine($"A batch of {numOfEvents} events has been published."); Console.ReadLine(); } finally { await producerClient.DisposeAsync(); }
-
Build the project, and ensure that there are no errors.
-
Run the program and wait for the confirmation message.
A batch of 3 events has been published.
[!NOTE] If you get an error "InvalidIssuer: Token issuer is invalid" when using Microsoft Entra authentication, it might be because the wrong Microsoft Entra Tenant ID is being used. In your code, replace 'new DefaultAzureCredential()' with 'new DefaultAzureCredential(new DefaultAzureCredentialOptions {TenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})' to explicitly specify Microsoft Entra Tenant ID.
[!IMPORTANT] If you're using the Passwordless (Microsoft Entra's Role-based Access Control) authentication, select Tools, then select Options. In the Options window, expand Azure Service Authentication, and select Account Selection. Confirm that you're using the account that was added to the Azure Event Hubs Data Owner role on the Event Hubs namespace.
-
On the Event Hubs namespace page in the Azure portal, you see three incoming messages in the Messages chart. Refresh the page to update the chart if needed. It might take a few seconds for it to show that the messages have been received.
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/verify-messages-portal.png" alt-text="Image of the Azure portal page to verify that the event hub received the events" lightbox="./media/getstarted-dotnet-standard-send-v2/verify-messages-portal.png":::
[!NOTE] For the complete source code with more informational comments, see this file on the GitHub
This section shows how to write a .NET Core console application that receives events from an event hub using an event processor. The event processor simplifies receiving events from event hubs.
In this quickstart, you use Azure Storage as the checkpoint store. Follow these steps to create an Azure Storage account.
- Create an Azure Storage account
- Create a blob container
- Authenticate to the blob container using either Microsoft Entra ID (passwordless) authentication or a connection string to the namespace.
[!INCLUDE storage-checkpoint-store-recommendations]
[!INCLUDE event-hub-storage-assign-roles]
Get the connection string to the storage account
Note down the connection string and the container name. You use them in the code to receive events from the event hub.
- In the Solution Explorer window, right-click the EventHubQuickStart solution, point to Add, and select New Project.
- Select Console application, and select Next.
- Enter EventHubsReceiver for the Project name, and select Create.
- In the Solution Explorer window, right-click EventHubsReceiver, and select Set as a Startup Project.
-
Select Tools > NuGet Package Manager > Package Manager Console from the menu.
-
In the Package Manager Console window, confirm that EventHubsReceiver is selected for the Default project. If not, use the drop-down list to select EventHubsReceiver.
-
Run the following command to install the Azure.Messaging.EventHubs and the Azure.Identity NuGet packages. Press ENTER to run the last command.
Install-Package Azure.Messaging.EventHubs Install-Package Azure.Messaging.EventHubs.Processor Install-Package Azure.Identity
-
Select Tools > NuGet Package Manager > Package Manager Console from the menu.
-
In the Package Manager Console window, confirm that EventHubsReceiver is selected for the Default project. If not, use the drop-down list to select EventHubsReceiver.
-
Run the following command to install the Azure.Messaging.EventHubs NuGet package:
Install-Package Azure.Messaging.EventHubs Install-Package Azure.Messaging.EventHubs.Processor
Replace the contents of Program.cs with the following code:
-
Replace the existing code in the
Program.cs
file with the following sample code. Then, replace the<STORAGE_ACCOUNT_NAME>
and<BLOB_CONTAINER_NAME>
placeholder values for theBlobContainerClient
URI. Replace the<EVENT_HUB_NAMESPACE>
and<HUB_NAME>
placeholder values for theEventProcessorClient
as well.Here are the important steps from the code:
- Creates an EventProcessorClient object using the Event Hubs namespace and the event hub name. You need to build BlobContainerClient object for the container in the Azure storage you created earlier.
- Specifies handlers for the ProcessEventAsync and ProcessErrorAsync events of the EventProcessorClient object.
- Starts processing events by invoking the StartProcessingAsync on the EventProcessorClient object.
- Stops processing events after 30 seconds by invoking StopProcessingAsync on the EventProcessorClient object.
using Azure.Identity; using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Consumer; using Azure.Messaging.EventHubs.Processor; using Azure.Storage.Blobs; using System.Text; // Create a blob container client that the event processor will use // TODO: Replace <STORAGE_ACCOUNT_NAME> and <BLOB_CONTAINER_NAME> with actual names BlobContainerClient storageClient = new BlobContainerClient( new Uri("https://<STORAGE_ACCOUNT_NAME>.blob.core.windows.net/<BLOB_CONTAINER_NAME>"), new DefaultAzureCredential()); // Create an event processor client to process events in the event hub // TODO: Replace the <EVENT_HUBS_NAMESPACE> and <HUB_NAME> placeholder values var processor = new EventProcessorClient( storageClient, EventHubConsumerClient.DefaultConsumerGroupName, "<EVENT_HUB_NAMESPACE>.servicebus.windows.net", "<HUB_NAME>", new DefaultAzureCredential()); // Register handlers for processing events and handling errors processor.ProcessEventAsync += ProcessEventHandler; processor.ProcessErrorAsync += ProcessErrorHandler; // Start the processing await processor.StartProcessingAsync(); // Wait for 30 seconds for the events to be processed await Task.Delay(TimeSpan.FromSeconds(30)); // Stop the processing await processor.StopProcessingAsync(); Task ProcessEventHandler(ProcessEventArgs eventArgs) { // Write the body of the event to the console window Console.WriteLine("\tReceived event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray())); Console.ReadLine(); return Task.CompletedTask; } Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs) { // Write details about the error to the console window Console.WriteLine($"\tPartition '{eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen."); Console.WriteLine(eventArgs.Exception.Message); Console.ReadLine(); return Task.CompletedTask; }
-
Replace the existing code in the
Program.cs
file with the following sample code. Then, replace the<AZURE_STORAGE_CONNECTION_STRING>
and<BLOB_CONTAINER_NAME>
placeholder values for theBlobContainerClient
URI. Replace the<EVENT_HUB_NAMESPACE_CONNECTION_STRING>
and<HUB_NAME>
placeholder values for theEventProcessorClient
as well.Here are the important steps from the code:
- Creates an EventProcessorClient object using the primary connection string to the namespace and the event hub. You need to build BlobContainerClient object for the container in the Azure storage you created earlier.
- Specifies handlers for the ProcessEventAsync and ProcessErrorAsync events of the EventProcessorClient object.
- Starts processing events by invoking the StartProcessingAsync on the EventProcessorClient object.
- Stops processing events after 30 seconds by invoking StopProcessingAsync on the EventProcessorClient object.
using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Consumer; using Azure.Messaging.EventHubs.Processor; using Azure.Storage.Blobs; using System.Text; // Create a blob container client that the event processor will use BlobContainerClient storageClient = new BlobContainerClient( "<AZURE_STORAGE_CONNECTION_STRING>", "<BLOB_CONTAINER_NAME>"); // Create an event processor client to process events in the event hub var processor = new EventProcessorClient( storageClient, EventHubConsumerClient.DefaultConsumerGroupName, "<EVENT_HUBS_NAMESPACE_CONNECTION_STRING>", "<HUB_NAME>"); // Register handlers for processing events and handling errors processor.ProcessEventAsync += ProcessEventHandler; processor.ProcessErrorAsync += ProcessErrorHandler; // Start the processing await processor.StartProcessingAsync(); // Wait for 30 seconds for the events to be processed await Task.Delay(TimeSpan.FromSeconds(30)); // Stop the processing await processor.StopProcessingAsync(); Task ProcessEventHandler(ProcessEventArgs eventArgs) { // Write the body of the event to the console window Console.WriteLine("\tReceived event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray())); return Task.CompletedTask; } Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs) { // Write details about the error to the console window Console.WriteLine($"\tPartition '{eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen."); Console.WriteLine(eventArgs.Exception.Message); return Task.CompletedTask; }
-
Build the project, and ensure that there are no errors.
[!NOTE] For the complete source code with more informational comments, see this file on the GitHub.
-
Run the receiver application.
-
You should see a message that the events have been received. Press ENTER after you see a received event message.
Received event: Event 1 Received event: Event 2 Received event: Event 3
These events are the three events you sent to the event hub earlier by running the sender program.
-
In the Azure portal, you can verify that there are three outgoing messages, which Event Hubs sent to the receiving application. Refresh the page to update the chart. It might take a few seconds for it to show that the messages have been received.
:::image type="content" source="./media/getstarted-dotnet-standard-send-v2/verify-messages-portal-2.png" alt-text="Image of the Azure portal page to verify that the event hub sent events to the receiving app" lightbox="./media/getstarted-dotnet-standard-send-v2/verify-messages-portal-2.png":::
You can use Azure Schema Registry to perform schema validation when you stream data with your Event Hubs SDK-based applications. Azure Schema Registry of Event Hubs provides a centralized repository for managing schemas and you can seamlessly connect your new or existing applications with Schema Registry.
To learn more, see Validate schemas with Event Hubs SDK.
This quick start provides step-by-step instructions to implement a scenario of sending a batch of events to an event hub and then receiving them. For more samples, select the following links.
- Event Hubs samples on GitHub
- Event processor samples on GitHub
- Azure role-based access control (Azure RBAC) sample
For complete .NET library reference, see our SDK documentation.
Delete the resource group that has the Event Hubs namespace or delete only the namespace if you want to keep the resource group.
See the following tutorial:
[!div class="nextstepaction"] Tutorial: Visualize data anomalies in real-time events sent to Azure Event Hubs