Tutorial for creating IOT Hub Triggered Azure Functions in Visual Studio using C#

Amie Barkes 61 Reputation points
2023-02-02T13:30:57.9733333+00:00

I am looking for a decent walk through to take me through the process of creating an IOT Hub Event triggered Azure function in Visual Studio including local testing and deployment to Azure.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,240 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,115 questions
{count} votes

3 answers

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 13,456 Reputation points
    2023-02-02T16:57:11.3966667+00:00

    Hello Amie, greetings! Welcome to Microsoft Q&A forum and thank you for posting this question. We would be glad to provide you the steps needed for this. Please note that the following process uses an Event Hub instance end point and not the default built in route for Azure IoT Hub. Please make sure you have an Event Hub end point instance created on the Azure portal and the IoT Hub data is successfully routed to the Event Hub instance to successfully test this process. You can create an Event Hub end point using the tutorial Create an event hub using Azure portal

    Follow the steps to develop, test and deploy the Azure Event Hub Trigger function in Visual Studio.

    Step 1 Create a new project in Visual Studio and select the Template as Azure Functions and follow the prompts.

    Screenshot_81

    Make sure to choose the Function type as Event Hub trigger and provide a Connection string setting name as shown below.

    User's image

    You will see that an Azure function with Event Hub Trigger gets generated.

    Step 2 Provide the connection string parameter in local.settings.json file and change method parameter as following.

    Change the Event Hub Trigger function parameter to provide an Event Hub name. Your method signature should look like this public static async Task Run([EventHubTrigger(eventHubName: "<eventhubinstancename", Connection = "ConnectionString")] EventData[] events, ILogger log)

    Navigate to the Event Hub instance on the Azure portal to copy the primary connection string. Refer the below image for details. If you do not have an access policy, you can create on to generate the keys.

    EventHubTrigger

    Open local.settings.json file and provide the connection string primary key we have copied from the above step as follows

    Screenshot_84

    Note The property name has to be the same as what we have defined for connection string setting name in step 1 during Azure Function creation. You would need to remove the EntityPath=<Event Hub Instance Name> at the end of the connection string as we already provided that in the function name.

    Step 3 Debug the code with a break point

    Make sure you have an application pushing data to the IoT Hub and routed to the Event Hub end point. You can use the quick start guide and an SDK to Send Telemetry data to the IoT Hub for testing purposes. I have a Python SDK that pushes values to the IoT Hub which is running on VS Code. Here is the telemetry it generates.

    User's image

    On the Event Hub Trigger function, I have put a break point where I am logging events. When I start the debugging of the application, notice the break point gets hit for every event delivered to the Event Hub.

    User's image

    Notice the events captured by the function.

    User's image

    Step 4 Deploy the function to the Azure cloud

    You can use the steps provided in the Publish the project to Azure section to guide you with deployment.

    Note Once the function is deployed you would have to create an application setting parameter to provide Connection string. Please refer the below image to help you add this setting.

    Function App deploy

    I still have my SDK running on the background pushing events to the Event Hub. I could see the deployed Azure function receive events and gets triggered. Please refer the below image.

    User's image

    Here are some additional resources that will help you to provide more details.

    1. Understand Azure IoT Hub routing
    2. Understanding different process models available for Azure functions

    Hope this helps you get started. Please let us know if you face any issues with any of the steps.


    • Kindly mark the answer as useful if the response is helpful so that it would benefit other community members facing the same issue. 
    • Original posters help the community find answers faster by identifying the correct answer. Here is how 
    • I highly appreciate your contribution to the community. 
    2 people found this answer helpful.

  2. Sander van de Velde 28,236 Reputation points MVP
    2023-02-02T17:19:14.7166667+00:00

    Hello @Amie Barkes ,

    Azure Functions supports Azure IoT Hub integration using a trigger for the 'event hub compatible endpoint' of the IoT Hub. This means you also have to consider adding a dedicated consumer group.

    The walk-through is simple, just a handful of steps...

    Here we go!

    I assume you know about how routing works for an IoT Hub.

    Add a separate consumer group:

    User's image

    Also, check the routing of the IoT Hub.

    If you add a custom route, the original eventhub endpoint turns into a fallback route.

    Repair this using that extra route:

    User's image

    Next, create a new Azure Function triggered by IoT Hub.

    Here, I added it in the portal but using a Visual Studio Azure Function project is recommended:

    User's image

    In the fields, select you IoT Hub and select the correct consumer group:User's image

    Once generated and telemetry arrives at the IoT Hub, the Azure Function will start ingesting the messages:

    User's image

    Give the Azure Function a few minutes to set up all underlying resources, this can take a few minutes!

    the connection credentials itself is stored in the Azure Function configuration Application settings:

    User's image

    There you see the same event-hub compatible endpoint as seen in the IoT Hub.

    The event hub name is not important for this connection. Please leave it as-is:

    User's image

    This is the way you add an IoT-Hub-triggered Azure Function by hand. Please try it out.

    For production, switch over to a project managed in Visual Studio or Visual Studio Code so the code is under version control and a DevOps pipeline can be added.

    The endpoint is a secret, please store it in an Azure KeyVault.


  3. Sander van de Velde 28,236 Reputation points MVP
    2023-08-22T21:31:20.9233333+00:00

    Hello @Amie Barkes ,

    based on earlier comments, it seems you try to send a message using this MessageToDevice dialog?

    User's image

    If so, this will never work the way to want to...

    The IoT Hub EventHub-compatible endpoint exposes device-to-cloud messages.

    These are messages sent by the device TO the IoT Hub.

    These messages can be picked up by any Azure service listening to this stream of messages, like an Azure Function.

    The dialog I see is used for CLOUD-TO-DEVICE messages.

    Usually, these will be sent using some application but you use this (very old, deprecated) Azure Device Explorer application. This can also be done with more modern tooling VS Code, the Azure IoT Explorer, or using the Azure Portal.

    Eventually, the message you sent is picked up by the device!

    The Azure IoT Hub offers two-way communication, two different kinds of communication techniques.

    So, you currently send a message to one device. And you are listening for messages coming from any device.

    Can you confirm this?

    If so, and coming back to your original question:

    ...including local testing and deployment to Azure.

    The IoT Hub can send messages to an event hub too. There you can pick up messages using an Azure Function (using another kind of trigger, listening to EventHub messages).

    If you want to simulate messages coming from the IoT Hub, Just write some code that sends simulated messages to the same event hub.

    The messages will look like this class. The only thing that differentiates IoT Hub messages from your messages will be the (lack of) system properties.

    Normally the simulation is done in a simpler way. In most projects we just have a couple of test devices (actual devices or just some applications) sending messages to the IoT Hub. Yes, you need to have a second IoT Hub or mix test devices with production devices in the same IoT Hub.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments