Azure Event Hubs “simplest” possible program

I want to connect several Netduino devices monitoring my house to a Microsoft Azure Event Hub, but the Netudino does not natively support https connections (required for the REST API) and AMQP looked a bit chunky.

The team at Kloud have some great posts about getting Arduino & NetMF devices connected using the Azure python SDK and an Application Request Routing based solution.

I’m going to build an ASP.NET MVC Web API 2 based Azure Event hub gateway. I aiming for a minimal install footprint and resource utilisation as I want to use my Essentials2012  home server or Windows 7 Media Centre box to host the gateway.

My first step is to create a the “simplest” possible program which connects to a Microsoft Azure Event Hub and uploads an event. (Minimal complexity, no async, no threads, and very flat), This console application uploads an event specified in a file to an Microsoft Azure Event Hub. The Azure service bus connection string is configured in the app.config file, the event hub name, file path and partition id are specified on the command line.

using System;
using System.IO;
using System.Configuration;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;

static void Main(string[] args)
{
   if (args.Length != 3)
   {
      Console.WriteLine("Incorrect number of arguments. Expected 3 args <eventhubname> <datafilepath> <partionkey>");
      return;
   }

   string eventHubName = args[0];
   string dataFilePath = args[1];
   string partitionKey = args[2];
   Console.WriteLine("Sending file {0} to EventHub {1} Partition {2}", dataFilePath, eventHubName, partitionKey);

   try
   {
      string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
      NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

      EventHubClient client = EventHubClient.Create(eventHubName);

      using (var dataFileStream = File.Open(dataFilePath, FileMode.Open))
      {
         EventData data = new EventData(dataFileStream)
         {
            PartitionKey = partitionKey,
         };

         // Set user properties if needed
         data.Properties.Add("Acquired", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"));

         DateTime startUtc = DateTime.UtcNow;
         client.Send(data);
         DateTime finishUtc = DateTime.UtcNow;
         TimeSpan duration = finishUtc - startUtc;
         Console.WriteLine("Duration {0:F2} secs", duration.TotalSeconds);
      }
   }
   catch (Exception ex)
   {
      Console.WriteLine("Send failed {0}", ex.Message);
   }
   Console.WriteLine("Press <Enter> to exit");
   Console.ReadLine();
   }
}

Uploading an event takes roughly 2.5 seconds on my ADSL internet connection.

One thought on “Azure Event Hubs “simplest” possible program

  1. Pingback: Azure Event Hub Service Gateway V0.1 | devMobile's blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s