Perform the following steps:
- Create a new console application that will send events to the Event Hub. I have named it EventHubApp.
- Run the following commands in the NuGet package manager to install the required libraries and interact with Azure Event Hubs:
Install-Package Microsoft.Azure.EventHubs
Install-Package Newtonsoft.Json
- Add the following namespaces and a reference to System.Configuration.dll:
using Microsoft.Azure.EventHubs;
using System.Configuration;
- Add the connection string to App.config, which is used to connect the event hub. This is the code for App.config. You can get the connection string by clicking on the ConnectionStrings link in the Overview tab of the event hub namespace:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0"
sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="EventHubConnection"
value="Endpoint=sb://<event hub namespace
here>.servicebus.windows.net/;Entitypath=<Event Hubname>;
SharedAccessKeyName= RootManageSharedAccessKey;
SharedAccessKey=<Key here>"/>
</appSettings>
</configuration>
- Create a new C# class file and place the following code in the new class file:
using System;
using System.Text;
using Microsoft.Azure.EventHubs;
using System.Configuration;
using System.Threading.Tasks;
namespace EventHubApp
{
class EventHubHelper
{
static EventHubClient eventHubClient = null;
public static async Task GenerateEventHubMessages()
{
EventHubsConnectionStringBuilder conBuilder = new
EventHubsConnectionStringBuilder
(ConfigurationManager.AppSettings
["EventHubConnection"].ToString());
eventHubClient =
EventHubClient.CreateFromConnectionString
(conBuilder.ToString());
string strMessage = string.Empty;
for (int nEventIndex = 0; nEventIndex <= 100;
nEventIndex++)
{
strMessage = Convert.ToString(nEventIndex);
await eventHubClient.SendAsync(new EventData
(Encoding.UTF8.GetBytes(strMessage)));
Console.WriteLine(strMessage);
}
await eventHubClient.CloseAsync();
}
}
}
- In your main function, place the following code, which invokes the method for sending the message:
namespace EventHubApp
{
class Program
{
static void Main(string[] args)
{
EventHubHelper.GenerateEventHubMessages().Wait();
}
}
}
- Now execute the application by pressing Ctrl + F5. You should see something similar to what is shown here:

- When the console is printing the numbers, you can navigate to the Azure Function to see that the event hub gets triggered automatically and logs the numbers that are being sent to the Event Hub.