Send and receive messages from Azure Event Hubs using .Net Part 5 – Create the app receiver to receive Azure Event Hub messages

Let’s continue the quickstart and create our receiver app. Right click and create a new project and place it in the same folder as the send project.

Use Nuget and install two new packages. Make sure that you are in the correct project when you use nuget.

Install-Package Azure.Messaging.EventHubs
Install-Package Azure.Messaging.EventHubs.Processor

Now it’s time to add some code.

Update the using with our new packages.

using System;
 using System.Text;
 using System.Threading.Tasks;
 using Azure.Storage.Blobs;
 using Azure.Messaging.EventHubs;
 using Azure.Messaging.EventHubs.Consumer;
 using Azure.Messaging.EventHubs.Processor;

Write constants for our connections.

private const string ehubNamespaceConnectionString = "";
     private const string eventHubName = "";
     private const string blobStorageConnectionString = "";
     private const string blobContainerName = "";

And update the main with our new .Net Core app.

static async Task Main()
     {
         // Read from the default consumer group: $Default
         string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;
     // Create a blob container client that the event processor will use      BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName);     // Create an event processor client to process events in the event hub     EventProcessorClient processor = new EventProcessorClient(storageClient, consumerGroup, ehubNamespaceConnectionString, eventHubName);     // Register handlers for processing events and handling errors     processor.ProcessEventAsync += ProcessEventHandler;     processor.ProcessErrorAsync += ProcessErrorHandler;     // Start the processing     await processor.StartProcessingAsync();     // Wait for 10 seconds for the events to be processed     await Task.Delay(TimeSpan.FromSeconds(10));     // Stop the processing     await processor.StopProcessingAsync(); }

And finally add event handler and error handler.

static async Task ProcessEventHandler(ProcessEventArgs eventArgs)
     {
         // Write the body of the event to the console window
         Console.WriteLine("\tRecevied event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));
     // Update checkpoint in the blob storage so that the app receives only new events the next time it's run     await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken); } static 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; }

Now run the receiver project and you will see the events you sent earlier.

To make things a bit more fun jump into your send project and update the code with some strings and also add a fourth event!

            // Add events to the batch. An event is a represented by a collection of bytes and metadata.              eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("My event")));             eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("in ")));             eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Azure Hub")));             eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Is here!")));             // Use the producer client to send the batch of events to the event hub             await producerClient.SendAsync(eventBatch);             Console.WriteLine("A batch of 4 events has been published.");

Now run both the send and the receive again and the output will be as below.

There you go! You’ve just created two .NET Core apps, storage accounts and an Azure Event Hub. Well done!

Send and receive messages with Azure Event Hubs using .NET Part 1 – Create an Event Hub

Let’s write some code and work with Azure Event Hubs. You can check out more about it here and here to view all the pre-requisites.

First jump over to the portal to create an Event Hub namespace.

Next go to the Event Hub namespace and create an event hub.

There you go! Next time we’ll download and add the event hub packages in visual studio.