To push events into Azure Event Hub you can use the Nuget-package WindowsAzure.ServiceBus (https://www.nuget.org/packages/WindowsAzure.ServiceBus/). That package works pretty straight forward, but there’s one problem:
It has been built for the .NET Framework. It is not available for .NET Core, which means you cannot use that package for example in an Universal Windows Platform (UWP) app or in a Xamarin-Forms app or in any other app that does not run on top of the full .NET Version.
But how do push events to your Azure Event Hub from an UWP-app? This question is exactly the one I had. I want to push events with sensor data from a Raspberry PI 3 to Azure Event Hub. And as the Raspberry PI 3 is running Windows IoT, I want to use an UWP-app. (Side note: I know there’s an Azure IoT Hub, but Event Hub can be interesting, as for specific scenarios it could be cheaper, and maybe it is given already in your architecture).
To access the Event Hub from an UWP-app, you can use
- use an Open Source-library. I tried a few, but they didn’t work on the latest build I was running
- use the REST-API of Azures Event Hub. And this is what this blog-post is about.
When you read about the REST-API, you’ll find out that there are two URIs to send an Event: With or without publisher name (or device Name, whatever you name it):
Now the challenge is that you need to handle the security with the Shared Access Signature Token. Somehow you Need to generate that token, and to do this, you also need to find out about your event hub namespace, your event hub name, your shared access key and finally hashing that stuff into a Shared Access Signature etc. And this seems to be complicated in an UWP. I haven’t found a trivial solution on the web, so I thought I build a generator for that token for you and for me.
Yesterday night when my wife and the kids where sleeping I switched Visual Studio to the Dark-theme – feeling like Batman – and I started to create a WPF-application that generates everything you need to access the REST-API of your Azure Event Hub. All you need to do is:
- Enter your Event Hub Connection String (you find that one on the Azure Portal)
- Enter a Token Lifetime (specify when the token will expire)
- Optionally Enter a Publisher Name (this can be a device name. Or you keep it empty to make REST-calls without publisher name)
The tool I created is called Event Hub Rest Client Generator, and it looks like this:
Now let’s see how to use it. The first thing is to access your connection string on the Azure Portal under http://manage.windowsazure.com. When you open up your event hub, you’ll see a link called “View Connection String”.
When you click the link, you find your connection strings (If you don’t see any, you need to configure SAS in your event hub). Now let’s copy one of these connection strings.
Let’s paste the connection string into the Event Hub Rest Client Generator and let’s specify a publisher name. Then the Generator will look like this:
As you can see, you get all the details, like for example the Shared Access Signature (of course including the HCMA SHA256 Hashvalue). You even get a generated sample class that is using the HttpClient to do REST-calls to your event hub. So you can just use that class to get started in your UWP-app to push events to Azure Event Hub via the REST-API.
The Event Hub Rest Client Generator is free, Open-Source and available on Github. Download the tool here:
Have fun and thanks for reading,