Sending and Receiving Events with Azure Event Hub using .Net Core 3.1

Sonu Kumar
5 min readJun 25, 2020

Background

Recently in a project I got a chance to explore Azure Event Hubs and I really liked it, it’s a brilliant service from Azure for some targeted scenarios. I thought to share it here in a simple way.

Event Hubs Architecture

Benefits of Event Hubs:

  1. Event Hub is a Azure service that can be used to take a large amount of data/events (can take or log millions of data or events per second nearly in real time).
  2. This large amount of data can be collected from web sites, client apps (mobile, web), IOT sensors and feeds from social networks.
  3. Can process/analyze this large amount of event data using any real-time analytics system like Apache Storm, Azure analytic system and so on.
  4. Store/transform this data into a data storage system including BLOB storage, hDInsight and Hadoop based solutions.

Scenarios :

Below are the scenarios where we can think to use Event Hub…

  1. Where we need to integrate/coordinate/receive millions of things (IoT Internet of Things), for example receive messages from millions of sensors, coordinate messages among thousands of trains or taxis and what not, send millions of logging messages to the cloud (on-premise tracking, monitoring or advertising centralization and more).
  2. Where we need to collect audit data from devices in the field.
  3. To collect the GPS locations of devices.

Event Handling Option in Azure :

Azure provides a variety of options to handle messages, events, and integration. Given below is a brief overview of some of the options available within Azure.

Prerequisites :

  • Visual Studio 2019
  • An active Azure subscription

Using the Code:

Step 1: Create an Event Hubs namespace and an event hub:

Go to azure portal , Select All services in the left menu and find Event Hubs as below.

select Add on the toolbar.

Enter the required details and then Select Review + Create at the bottom of the page.

It will create Azure namespace as below :

Create an event hub

To create an event hub within the namespace, do the following actions:

  1. On the Event Hubs Namespace page, select Event Hubs in the left menu.
  2. At the top of the window, click + Event Hub.
  3. Type a name for your event hub, then click Create

Step 2: Get connection string from the portal:

On the Event Hubs Namespace page, select Shared Access Policies on the left menu and enter the required details.

After creating the shared access policies, we will get connection string details as below :

We need to copy the above primary connection string for future use.

Step 3: Create a storage account for Event Processor Host:

The Event Processor Host is an intelligent agent that simplifies receiving events from Event Hubs by managing persistent checkpoints and parallel receives. For checkpointing, the Event Processor Host requires a storage account :

From the Azure portal menu, select Create a resource. Then Select Storage > Storage account.

Enter required details and On the Review + create page, review the values, and select Create.

After you see the Deployments Succeeded message in your notifications, select Go to resource to open the Storage Account page. Alternatively, you can expand Deployment details and then select your new resource from the resource list.

Select Containers and Select + Container at the top, enter a Name for the container, and select OK.

It will create container inside azure storage as below :

Step 4: Create a console application to send event to event hub :

Create an console app to send event to event hub.

Add the Microsoft.Azure.EventHubs .NET Core library NuGet package to your project and specify EventHubConnectionString and EventHubName value .

When executing client app, output is something below :

Step 5: Create a console receiver application to receive event from event hub:

write a .NET Core console application that receives messages from an event hub using the Event Processor Host. The Event Processor Host is a .NET class that simplifies receiving events from event hubs by managing persistent checkpoints and parallel receives from those event hubs. Using the Event Processor Host, you can split events across multiple receivers, even when hosted in different nodes. This example shows how to use the Event Processor Host for a single receiver.

Create a console receiver app and Add the Microsoft.Azure.EventHubs and Microsoft.Azure.EventHubs.Processor .NET .

we need to Implement the IEventProcessor interface in this project.

When executing receiver app, output is something below :

Hence our job is done !!!

Conclusion

In this article, We have looked at how to Send events to or receive events from Azure Event Hubs using .NET Core (Microsoft.Azure.EventHubs)

Has this article been useful to you? please share extensively, we also welcome feedback on content you would like us to cover .

--

--

Sonu Kumar

Software Consultant interested in Microservices / Serverless computing, Middleware / SOA, Event Driven Architecture & Machine Learning.