Calling the Azure Event Hub REST-API – from UWP, WPF and any other .NET (Core) Client

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 to 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

  • an Open Source-library. I tried a few, but they didn’t work on the latest build I was running
  • 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):

  • https://{servicebusNamespace}.servicebus.windows.net/{eventHubPath}/messages
  • https://{servicebusNamespace}.servicebus.windows.net/{eventHubPath}/publishers/{publisherId}/messages

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-app. 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:

  1. Enter your Event Hub Connection String (you find that one on the Azure Portal)
  2. Enter a Token Lifetime (specify when the token will expire)
  3. 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:

calling-the-azure-event-hub-rest-api-from-uwp-wpf-and-any-other-net-core-client_1

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”.

calling-the-azure-event-hub-rest-api-from-uwp-wpf-and-any-other-net-core-client_2

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.

calling-the-azure-event-hub-rest-api-from-uwp-wpf-and-any-other-net-core-client_3

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:

calling-the-azure-event-hub-rest-api-from-uwp-wpf-and-any-other-net-core-client_4

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:

https://github.com/thomasclaudiushuber/EventHub.RestClientGenerator/releases

Have fun and thanks for reading,

Thomas

Share this post

Comments (4)

  • Manu Reply

    Just used this to send events from Xamarin.Forms to an Azure EventHub. Worked like a charm! Saved my some SAS fiddling… Thanks! :-)

    May 25, 2016 at 7:41 pm
  • Thomas Claudius Huber Reply

    Hey Manu, great it was useful for you. :-)

    June 28, 2016 at 12:30 pm
  • Sami Reply

    Hello Thomas,
    I have tried this to generate the EventHubClient class and call PostDataAsync, the first call worked and hung at this line: httpClient.PostAsync(…), how can I fix this? Thanks.

    May 18, 2018 at 11:32 am

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.