In this article, I am going to demonstrate how to send the events to the Event Hub using Visual Studio. In my previous
article, I showed how to create an Event Hub on the Azure portal, stream analytics input, and output of the events from the console application. An application, devices, gateways, and port can send events to Azure Event Hub using the Azure Events Hub REST API.
Prerequisites
- An Azure Subscription.
- Visual Studio 2015 or higher with an internet connection.
Follow the below steps to send an event to the Event Hub.
Step 1
Open Visual Studio 2017 and press File-->New-->Project or click Ctrl+Shift+N to open the New Project.
Step 2
Under Visual C#-->Classic Window Desktop-->Console App(.NET Framework), enter the desired name for your project and press OK.
Step 3
In the Solution Explorer, right-click your project and select the "Manage NuGet Packages" option.
Step 4Click "Browse" and search for the package named WindowsAzure.ServiceBus. From the results, select the required package and click "Install" to install the package in our application. This package contains the files that our app will use to send events to the Event Hub.
And also, search for the Newtonsoft.Json and click "Install" to install the package in our application. This package contains the APIs for creating and consuming JSON.
Step 5Copy and replace the code in the program.cs file. Change the 16th line of this code with the Connection string that is copied from your Azure Event hub.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.ServiceBus.Messaging;
- using Newtonsoft.Json;
-
- namespace DemoEventGenerator
- {
- class Program
- {
- static double _probability = 0.01;
- static int _transactions = 0;
- static int _cardNumber = -1;
- static string _connectionString = "Connection String";
-
- static void Main(string[] args)
- {
- var rand = new Random();
- var client = EventHubClient.CreateFromConnectionString(_connectionString , "demoinputhub");
-
- while (true)
- {
- int card = 123456789 + rand.Next(0, 888888888);
-
-
- if (rand.NextDouble() < _probability && _cardNumber != -1)
- {
- card = _cardNumber;
- _cardNumber = -1;
- }
-
-
- var transaction = new
- {
- transactionId = _transactions++,
- transactionTime = DateTime.UtcNow.ToString(),
- deviceId = 12345 + rand.Next(0, 88888),
- cardNumber = card,
- amount = rand.Next(1, 20) * 20
- };
-
-
- if (rand.NextDouble() < _probability)
- {
- _cardNumber = transaction.cardNumber;
- }
-
-
- var message = JsonConvert.SerializeObject(transaction);
- client.Send(new EventData(Encoding.UTF8.GetBytes(message)));
- Console.WriteLine("[{0}] Event transmitted", transaction.transactionId);
- }
- }
- }
- }
Step 6
Click Ctrl+F5 or press "Start" to run the program and ensure that the output seems like the following. Each line of the output represents one event sent to the Event Hub at the rate of several seconds.
Summary
I hope you understood the process of sending an event to the Events Hub. This demo generator simulates these in the software.