locked
Azure IoT Hub, EventHub and Functions RRS feed

  • Question

  • I have an IoTHub with a route that points to an EventHub which triggers a Functions.
    I'm having problem getting the *DeviceId* and other IoT Hub properties from the event object without adding those explicitly to the payload.

    If I set the input type to a string (or a custom type):

    public static void Run(string iotMessage, TraceWriter log)
    {
        log.Info($"C# Event Hub trigger function processed a message: {iotMessage}");
    }


    I only get the payload without any other IoT Hub properties like DeviceId, CorrelationId or MessageId.

    I tried to set the type to EventData instead:
       
    public static void Run(EventData iotMessage, TraceWriter log) 
    {
        log.Info($"C# Event Hub trigger function processed a message: {JsonConvert.SerializeObject(iotMessage)}");
    }


    Now I can access the IoT Hub properties via two getters: Properties and SystemProperties. For example I can access DeviceId like this:
    iotMessage.SystemProperties["iothub-connection-device-id"]

    But it does not expose the payload.
    So how do I access both IoT Hub properties and the payload?

    Joakim Rosendahl Consultant at Active Solution, Sweden.

    Thursday, February 2, 2017 2:01 PM

Answers

  • I missed something in the documentation for EventData. It has a method called GetBytes() and returns the body as a byte array. Example of getting both the IoT Hub properties and the body:

    public static async void Run(EventData telemetryMessage, TraceWriter log)
    {
        var deviceId = GetDeviceId(telemetryMessage);
        var payload = GetPayload(telemetryMessage.GetBytes());
    
        log.Info($"C# Event Hub trigger function processed a message.  deviceId: { deviceId }, payload: { JsonConvert.SerializeObject(payload) }");
    }
    
    private static Payload GetPayload(byte[] body)
    {
        var json = System.Text.Encoding.UTF8.GetString(body);
        return JsonConvert.DeserializeObject<Payload>(json);
    }
    
    private static string GetDeviceId(EventData message)
    {
        return message.SystemProperties["iothub-connection-device-id"].ToString();
    }

    I guess this is the solution, otherwise any from the MS team can give me better example.


    Joakim Rosendahl Consultant at Active Solution, Sweden.

    Thursday, February 2, 2017 3:13 PM

All replies

  • I missed something in the documentation for EventData. It has a method called GetBytes() and returns the body as a byte array. Example of getting both the IoT Hub properties and the body:

    public static async void Run(EventData telemetryMessage, TraceWriter log)
    {
        var deviceId = GetDeviceId(telemetryMessage);
        var payload = GetPayload(telemetryMessage.GetBytes());
    
        log.Info($"C# Event Hub trigger function processed a message.  deviceId: { deviceId }, payload: { JsonConvert.SerializeObject(payload) }");
    }
    
    private static Payload GetPayload(byte[] body)
    {
        var json = System.Text.Encoding.UTF8.GetString(body);
        return JsonConvert.DeserializeObject<Payload>(json);
    }
    
    private static string GetDeviceId(EventData message)
    {
        return message.SystemProperties["iothub-connection-device-id"].ToString();
    }

    I guess this is the solution, otherwise any from the MS team can give me better example.


    Joakim Rosendahl Consultant at Active Solution, Sweden.

    Thursday, February 2, 2017 3:13 PM
  • Got an answer on StackOverflow regarding this and this is the recommended way for now.

    Joakim Rosendahl Consultant at Active Solution, Sweden.

    Friday, February 3, 2017 7:02 AM