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.
- using System.Threading;
- 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.
- static string eventHubName = "{Event Hub name}";
- static string connectionString = "{send connection string}";
Step 15
The below mentioned methods should also be added to the class of the program.
- static void SendingRandomMessages() {
- var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
- while (true) {
- try {
- var message = Guid.NewGuid().ToString();
- Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, message);
- eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
- } catch (Exception exception) {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
- Console.ResetColor();
- }
- Thread.Sleep(200);
- }
- }
Step 16
Add the following commands to the main method of the program structure.
- Console.WriteLine("Press Ctrl-C to stop the sender process");
- Console.WriteLine("Press Enter to start now");
- Console.ReadLine();
- SendingRandomMessages();
Step 17
Press F5 or run the console application. We have sent the messages to the event hub.
Thanks for reading!