Send events to Azure Event Hubs using the .NET Framework

Introduction

Event Hubs is a service that processes large amounts of event data (telemetry) from connected devices and applications. After you collect data into Event Hubs, you can store the data using a storage cluster or transform it using a real-time analytics provider. This large-scale event collection and processing capability is a key component of modern application architectures including the Internet of Things (IoT).

This tutorial shows how to use the Azure portal to create an event hub. It also shows how to send events to an event hub using a console application written in C# using the .NET Framework. To receive events using the .NET Framework, see the Receive events using the .NET Framework article, or click the appropriate receiving language in the left-hand table of contents.

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 event hub, follow the procedure in this article, then proceed with the following steps in this tutorial.

Create a sender console application

In this section, you write a Windows console app that sends events to your event hub.

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

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

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

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

    using System.Threading;
    using Microsoft.ServiceBus.Messaging;
    
  5. 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.

    static string eventHubName = "{Event Hub name}";
    static string connectionString = "{send connection string}";
    
  6. Add the following method to the Program class:

    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);
       }
    }
    

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

  7. 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");
    Console.ReadLine();
    SendingRandomMessages();
    
  8. Run the program, and ensure that there are no errors.

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

Next steps

Now that you've built a working application that creates an event hub and sends data, you can move on to the following scenarios: