Sending Events To Event Hubs In Azure

Introduction

In this article, I will explain about sending the events to Event Hubs in Azure using .NET framework. The Event Hubs has the capacity of processing and storing the data in data streaming platform and can process millions of processes per second with low latency. It also acts as a ramp for big data.

Prerequisites

  • An active Azure subscription
  • Visual Studio 2017

Sending the events to Event Hubs

Before proceeding to send the events to the Event Hubs in Azure, we need to create a Event Hub so that we can send the events to it.

Step 1

Log into Azure portal.

Step 2

Click New -> Internet of Things -> Event Hubs.

Step 3

Now, we need to configure some basic settings, provide a name for the namespace, create a resource group with the location of the datacenter, and click "Create".

Step 4

From the namespace blade that we created, click Overview -> Event Hub.

Step 5

Provide a name for the Event Hub and click "Create".

Step 6

Select the created new event hub name from the list of the Event Hubs.

Step 7

Click "Shared access policies" and select "RootManagerSharedAccessKey".

Step 8

Now, we need to backup a copy of the primary key and click Copy button in RootManageSharedAccessKey to clipboard.

Step 9

Now, the event hub is created. For sending the events to the Azure, open Visual Studio 2017.

Step 10

Click New -> Windows Classic Desktop -> Console App (.NET Framework) and click OK.

Step 11

The event hub process needs to be added with NuGet packages and right click Project.

Step 12

Click -> Manage NuGet Packages -> and in the Browse tab, search for MicrosoftAzureServiceBus. Click "Install".

Step 13

Some of the following using statements must be added to the program files.

  1. using System.Threading;  
  2. using Microsoft.ServiceBus.Messaging;   

Step 14

Again, add the following values to the event hub and to the namespace connection string. Replace the event hub name and connection string that has been created in Azure portal. 

  1. static string eventHubName = "{Event Hub name}";  
  2. static string connectionString = "{send connection string}";   

Step 15

The below mentioned methods should also be added to the class of the program. 

  1. static void SendingRandomMessages() {  
  2.     var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);  
  3.     while (true) {  
  4.         try {  
  5.             var message = Guid.NewGuid().ToString();  
  6.             Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, message);  
  7.             eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));  
  8.         } catch (Exception exception) {  
  9.             Console.ForegroundColor = ConsoleColor.Red;  
  10.             Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);  
  11.             Console.ResetColor();  
  12.         }  
  13.         Thread.Sleep(200);  
  14.     }  
  15. }   

Step 16

Add the following commands to the main method of the program structure. 

  1. Console.WriteLine("Press Ctrl-C to stop the sender process");  
  2. Console.WriteLine("Press Enter to start now");  
  3. Console.ReadLine();  
  4. SendingRandomMessages();   

Step 17

Press F5 or run the console application. We have sent the messages to the event hub.

Thanks for reading!


Similar Articles