How to send serialized object to azure iot hub?

Jacob Krauth 21 Reputation points

Hi there,

i am using an instance of the azure iot hub basic with an endpoint routing to a service bus queue.
To reduce the message size i want to send my data as a binary serialized object instead of a json serialized text converted afterwards to a byte stream.
After routing the message to the service bus queue i want to trigger an azure function to deserialize the binary content to an custom typed object.
It is possible to receive a Message obejct in the azure function with all system properties of the message but the body containing the binary data is empty.

Properties of my issue:
Azure IoT Hub - B1
Azure function C# .NET 6.0

Thanks for help!

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
556 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,353 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,134 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sander van de Velde 29,461 Reputation points MVP

    Hello @Jacob Krauth ,

    you want to send a byte array to the IoT Hub and decode this byte array in Azure using an Azure Function.

    This is fine :-)

    Almost all examples are working with JSON, but did you notice the JSON inside the messages are first converted to a bytearray and then decoded in the cloud?

    Or, did you notice the default Azure Function template for the IoT Hub exposes a string? Which is just the decoded body of incoming messages?

    So, let's see how this can be fixed.

    First, you send a message with just a byte array. In this blog post

     string jsonData = "{ \"single\":true }";           
     using var message = new Message(Encoding.UTF8.GetBytes(jsonData));  

    You can just do:

     using var message = new Message(yourByteArray));  

    From here, Azure will handle this message still well but all tooling, like the Azure IoT Explorer, expects JSON so that will not work well.

    Now, the message is ingested and you want to transform it.

    Here is an example of an Azure Function directly connected to the IoT Hub.

    Note: The post is about not telemetry messages but that is ok. The incoming message type is still the same: 'EventData'.

    As you can see, the type of the incoming parameter is not a string, it's that Event Data:

    public static void Run(EventData myIoTHubMessage, ILogger log)  
      log.LogInformation($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");  
      var bodyText = Encoding.UTF8.GetString(myIoTHubMessage.Body);  

    In that example, the body of the message (now your byte array) could be encoded.

    But all you need to do is:

    var bodyByteArray = myIoTHubMessage.Body;  

    Now, it's up to you to do something fancy with the byte array.

    Extra: I did something similar with compressing data. There, I alter the ContentType of the message. Perhaps you have to play with that value (like 'application/octet-stream').

    0 comments No comments

0 additional answers

Sort by: Most helpful