Sending An Event To The Event Hub Using Microsoft Visual Studio

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 4

Click "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 5

Copy 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.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.ServiceBus.Messaging;  
  7. using Newtonsoft.Json;  
  8.   
  9. namespace DemoEventGenerator  
  10. {  
  11.     class Program  
  12.     {  
  13.         static double _probability = 0.01;  
  14.         static int _transactions = 0;  
  15.         static int _cardNumber = -1;  
  16.         static string _connectionString = "Connection String";  
  17.   
  18.         static void Main(string[] args)  
  19.         {  
  20.             var rand = new Random();  
  21.             var client = EventHubClient.CreateFromConnectionString(_connectionString , "demoinputhub");  
  22.   
  23.             while (true)  
  24.             {  
  25.                 int card = 123456789 + rand.Next(0, 888888888);  
  26.   
  27.                 // Occasionally generate a fraudulent transaction by reusing a card number  
  28.                 if (rand.NextDouble() < _probability && _cardNumber != -1)  
  29.                 {  
  30.                     card = _cardNumber;  
  31.                     _cardNumber = -1;  
  32.                 }  
  33.   
  34.                 // Formulate a transaction  
  35.                 var transaction = new  
  36.                 {  
  37.                     transactionId = _transactions++,  
  38.                     transactionTime = DateTime.UtcNow.ToString(),  
  39.                     deviceId = 12345 + rand.Next(0, 88888),  
  40.                     cardNumber = card,  
  41.                     amount = rand.Next(1, 20) * 20  
  42.                 };  
  43.   
  44.                 // Occasionally record a card number for later use in generating fraud  
  45.                 if (rand.NextDouble() < _probability)  
  46.                 {  
  47.                     _cardNumber = transaction.cardNumber;  
  48.                 }  
  49.   
  50.                 // Send an event to the event hub  
  51.                 var message = JsonConvert.SerializeObject(transaction);  
  52.                 client.Send(new EventData(Encoding.UTF8.GetBytes(message)));  
  53.                 Console.WriteLine("[{0}] Event transmitted", transaction.transactionId);  
  54.             }  
  55.         }  
  56.     }  
  57. }  
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. 


Similar Articles