我们最近升级了 Service Fabric 服务,该服务从 Azure IoT 中心内置事件中心读取消息,从已弃用的 Microsoft.Azure.EventHubs SDK 升级到 Azure.Messaging.EventHubs SDK(遵循本迁移指南)。
我们遇到了一个问题,即 EventData 类的 SystemProperties 未完全填充。具体来说,message-id、correlation-id和user-id属性没有数据,如下所示:
{"message-id":{},"user-id":{"Length":0,"IsEmpty":true},"correlation-id":{},"iothub-connection-device-id": "xxxxxxx","iothub-connection-auth-method":"{"scope":"device","type":"sas","issuer":"iothub","acceptingIpFilterRule":null}","iothub -connection-auth- Generation-id":"xxxxxxxxxxxxxxxxx","iothub-enqueuedtime":"2023-07-13T22:00:53.501Z","iothub-message-source":"遥测","x-opt-序列号":4785508,"x-opt-offset":1005023123136,"x-opt-enqueued-time":"2023-07-13T22:00:53.652+00:00"}
我们使用 EventProcessorClient 和 AmqpTcp 传输类型。
任何人都可以帮助我们确定为什么这些系统属性没有被填充吗?这以前适用于已弃用的 SDK。
如有任何帮助,我们将不胜感激。
根据此 MSDOC,通过事件中心服务接收的事件已填充这些特征。从其他集合收到的事件为空。
static async Task ReceiveMessagesFromEventHub()
{
await using var consumerClient = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, connectionString, eventHubName);
await foreach (PartitionEvent partitionEvent in consumerClient.ReadEventsAsync())
{
EventData eventData = partitionEvent.Data;
object messageId = eventData.Properties["message-id"];
object correlationId = eventData.Properties["correlation-id"];
object userId = eventData.Properties["user-id"];
object Iotubconectiondevidid = eventData.Properties["iothub - connection - device - id"];
object iothubconnectionauthmethod = eventData.Properties["iothub - connection - auth - method"];
请参阅 this 了解缺少的 IoT 中心系统 Properties 和 SO。
来自物联网中心的事件:
来自事件中心的事件:
此外,对于遇到此问题的任何人,我还发现了与我原来的问题相关的问题,因为我们使用了一些也未填充的自定义属性(带有EventData.Properties)。
事实证明,属性字典键是区分大小写的,而在我们升级到 System.Messaging.EventHubs 之前,它们不是区分大小写的。我知道这一点是因为我有两组设备向 Azure IoT 中心发送消息(一组使用全小写属性密钥,一组使用区分大小写的属性密钥),并且它们在升级之前都可以正常工作。
调整我的代码来检索这些属性值以通过不区分大小写的键进行检索修复了问题,以及@Sampath接受的其他答案。