Azure IoT Hub, EventHub and Functions

橙三吉。 提交于 2019-12-01 04:15:31

That's the recommended way of doing this, if you need access to detailed event properties in addition to the payload. The simple/default bindings for string etc. are useful in cases where you don't need to access those event properties. Our runtime calls EventData.GetBytes() for you behind the scenes and converts the data to the input type you've specified.

I do think we could make improvements to facilitate these scenarios however. I've logged a bug here in our repo to track this.

I missed a thing 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();
}

There are some upcoming updates that will simply this according to this:

Added first class binding data support for many of the important ServiceBus and EventHub message/event properties. For EventHub:

  • PartitionContext
  • PartitionKey
  • Offset
  • SequenceNumber
  • EnqueuedTimeUtc
  • Properties
  • SystemProperties

For ServiceBus:

  • DeliveryCount
  • DeadLetterSource
  • ExpiresAtUtc
  • EnqueuedTimeUtc
  • MessageId
  • ContentType
  • ReplyTo
  • SequenceNumber
  • To
  • Label
  • CorrelationId
  • Properties

So you should be able to bind to these properties as well as the payload.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!