Send events to Azure Event Hubs using the .NET Framework

Azure Event Hubs is a Big Data streaming platform and event ingestion service, capable of receiving and processing millions of events per second. Event Hubs can process and store events, data, or telemetry produced by distributed software and devices. Data sent to an event hub can be transformed and stored using any real-time analytics provider or batching/storage adapters. For detailed overview of Event Hubs, see Event Hubs overview and Event Hubs features.

This tutorial shows how to send events to an event hub using a console application written in C# using the .NET Framework.


To complete this tutorial, you need the following prerequisites:

Create an Event Hubs namespace and an event hub

The first step is to use the Azure portal to create a namespace of type Event Hubs, and obtain the management credentials your application needs to communicate with the event hub. To create a namespace and an event hub, follow the procedure in this article, then proceed with the following steps in this tutorial.

Get the connection string for the event hub namespace by following instructions from the article: Get connection string. You use the connection string later in this tutorial.

Create a console application

In Visual Studio, create a new Visual C# Desktop App project using the Console Application project template. Name the project Sender.

Create console application

Add the Event Hubs NuGet package

  1. In Solution Explorer, right-click the Sender project, and then click Manage NuGet Packages for Solution.
  2. Click the Browse tab, then search for WindowsAzure.ServiceBus. Click Install, and accept the terms of use.

    Install Service Bus NuGet package

    Visual Studio downloads, installs, and adds a reference to the Azure Service Bus library NuGet package.

Write code to send messages to the event hub

  1. Add the following using statements at the top of the Program.cs file:

    using System.Threading;
    using Microsoft.ServiceBus.Messaging;
  2. Add the following fields to the Program class, substituting the placeholder values with the name of the event hub you created in the previous section, and the namespace-level connection string you saved previously. You can copy connection string for your event hub from Connection string-primary key under RootManageSharedAccessKey on the Event Hub page in the Azure portal. For detailed steps, see Get connection string.

    static string eventHubName = "Your Event Hub name";
    static string connectionString = "namespace connection string";
  3. Add the following method to the Program class:

    static void SendingRandomMessages()
        var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
        while (true)
                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);

    This method continuously sends events to your event hub with a 200-ms delay.

  4. Finally, add the following lines to the Main method:

    Console.WriteLine("Press Ctrl-C to stop the sender process");
    Console.WriteLine("Press Enter to start now");
  5. Run the program, and ensure that there are no errors.

Congratulations! You have now sent messages to an event hub.

Next steps

In this quickstart, you have sent messages to an event hub using .NET Framework. To learn how to receive events from an event hub using .NET Framework, see Receive events from event hub - .NET Framework.