Manually connect and send data to an Azure Event Hub

Update 04.02.2017: As the Microsoft.Azure.EventHubs NuGet package since two days finally support every .NET platform, this post go obsolete.

Azure Event Hubs provide a neat way to collect loads of data that is pushed against them and holds it until its processing. Microsoft offers an SDK to work with Event Hubs for most platforms including .NET, of course. Unfortunately, the provided NuGet package does not work on all .NET platforms, so when targeting UWP for example, you have to implement the Event Hub connection by yourself, which can be a little bit tricky. Here is how to manually connect and send data to an Azure Event Hub.

Get the connection information

Once the Event Hub has been created, open the Service Bus section in the Azure Portal and navigate to your Event Hub. Select the Configure tab and scroll down to the Shared access policies section. Make sure, you created a policy that has the permission to send data to the Event Hub and note its Name and Primary Key. You need both later.

Besides, we need the base address of the Event Hub, that we can find at the Dashboard tab. The base address is everything before the first slash. In my case it’s

Azure Event Hub Shared Access Keys

Establish a connection

Now you are ready to connect. For better separation and reusability, I have created an EventHubService class in my project. You will need the following variables:

With these you are able to authenticate at the REST endpoint of your Event Hub. You need to authenticate to send data and identify yourself so make sure, that you call the authentication process at least once. To authenticate, I created the following method that you can simply copy. It defines an expiry for the access token, which is mandatory and renders the Primary Key to a SHA256 hash that gets the expiry date appended. With this hash we can generate a token with all the other values. This token will be set as the Authorization header of the HttpClient and will be used for the next requests.

Send data to the Event Hub

As soon as you are authenticated, data can be sent. For this, I recommend writing another simple method that simply transforms an object into JSON and sends it to the Event Hub. For the JSON transformation, I used the popular JSON.NET NuGet package. Once the content is in the right format, you can create a StringContent out of it and send it to the /messages endpoint of your Event Hub. That’s it.

Sometimes, it might happen, that you get a 401 error (unauthorized) as response. If this happens, your access  token might be expired. That’s why I simply regenerate it and try to send the request again when this happens, as you can see below.

You can find the full code at GitHub. Hope that helps!

Leave a Reply