Azure IoT Hub, EventHub and features

I have an IoTHub with a route that points to an EventHub that runs the functions.

I had a problem getting the DeviceId properties and other IoT Hub objects from the event object without explicitly adding them to the payload.

If I set the input type to 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 get the payload without any other IoT Hub properties like DeviceId, CorrelationId or MessageId.

I tried to set the type EventDatainstead:

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 IoT Hub properties through two getters: Properties and SystemProperties. For example, I can access DeviceId, like this one iotMessage.SystemProperties["iothub-connection-device-id"]. But it does not reveal the payload.

, IoT Hub ?

+6
3

- EventData. GetBytes() . IoT Hub :

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();
}
0

, . / string .. , . EventData.GetBytes() .

, . , .

+1

, :

​​ ServiceBus / EventHub. EventHub:

  • PartitionContext
  • PartitionKey
  • SequenceNumber
  • EnqueuedTimeUtc
  • SystemProperties

ServiceBus:

  • DeliveryCount
  • DeadLetterSource
  • ExpiresAtUtc
  • EnqueuedTimeUtc
  • MessageId
  • ContentType
  • ReplyTo
  • SequenceNumber
  • CorrelationId

Therefore, you should be able to communicate with these properties as well as the payload.

+1
source

Source: https://habr.com/ru/post/1014649/


All Articles