Getting Started with Azure Event Hub and IOT Hub Triggers using Visual Studio 2019

After seeing quite a few questions regarding writing code for Azure Event Hub on the forums, I thought would be a good time to write a post to share my experience and help whoever is getting started… or getting stuck.

Developing code locally with Visual Studio depends on the emulator for debugging and sometimes that will offer some challenges.

Let’s first go through developing some code for the Azure Even Hub from scratch and later discuss some interesting points.

If you don’t have a hub follow this to create one: https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-create

Creating a Hub in portal is very straightforward, so I won’t spend time on that. Leave a comment if you need help

Once you have a hub up and running, launch your Visual Studio 2019 (will cover VS Code on another post).

Make Sure your Visual Studio has no pending updates.

Select the “Event Hub Trigger” template. I recommend you select a Storage Account to avoid some distracting errors. The errors won’t stop it from pulling events but if you don’t add you will get a missing storage error.

On the example below we are creating a Azure Function V3 Dot Net Core 3.

If you want to save your connection string into the local.settings.json you can skip  entering a connection string for now. This field (image below) is good for the old approach where we store the string on the function.

Once you click next you should get this code using the outdated “Microsoft.Azure.EventHubs”. Even after the Visual studio upgrade I still get outdated code. May need to do some azure sdk upgrade or wait for the azure team to release an upgrade. We need to replace with the new  and tweak the connection to look for it in the “local.settings.json”

First fire the nuget console to add “Azure.Messaging.EventHubs”

After adding the new library, change the code to this:

Now, Since we are creating a trigger that fires when a event hits the hub, the connection string we use is the one for the “hub entity”, not the “hub namespace”

Not this:

This one:

Add the connection string to the

Now when you hit debug you should see this:

Case you get an error like: “The listener for function ‘EventHubTriggerCSharp’ was unable to start. Microsoft.Azure.EventHubs.Processor: Encountered error while fetching the list of EventHub PartitionIds. System.Private.CoreLib: An attempt was made to access a socket in a way forbidden by its access permissions.“, check my other post to find how to fix it: Azure Event Hub error: “An attempt was made to access a socket in a way forbidden by its access permissions” – Bruno Lucas Azure Blog

If after all you still get an error, download this sample (BrunoLucasBlog/AzureHubDemoVS2019) from my github, replace with your connection string and try to run. Sometimes you’ve done all the above and could be a package version or some very hard to find detail that may consume a lot of time to find. Try comparing with a working project.

IOT HUB

The IOT hub is very similar. There is a template for that in VS 2019

The bigger difference is the anatomy of the IOT Hub.

The connection string is here:

The trigger has a different name and the connection string is the IOT Hub Built-in endpoint. The rest is pretty much the same.

If we Check the official Microsoft Documentation it mentions the Event Hub Trigger syntax should work for the IOT Hub.

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-iot-trigger?tabs=csharp

I gave it a try and it did work

I hope this help whoever is getting stuck during these times Azure upgrades are moving on a different pace from Visual Studio. Next post will be exploring Visual Studio code. One thing you will need along with the fully functioning emulator debugging is a tool to send messages. You can write code but the tools are a bit more flexible. Some offer really cool features to pick inside the hub. Like this old but still good open source one:

https://github.com/paolosalvatori/ServiceBusExplorer

Never tried the above on a IOT hub but VS code has this one specific for the IOT hub:

https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit

And this one for the good old event hub:

https://marketplace.visualstudio.com/items?itemName=Summer.azure-event-hub-explorer

Next post will be discussing the differences and benefits between azure function triggers and Logic Apps triggers. All of the above is far much simpler in Logic Apps.

Leave a Reply