我尝试使用“EventHubReceiver”(Device2Cloud)从设备接收消息。每个设备都应该有自己的单个接收器。
为所有设备创建一个 EventHubReceiver(每个分区)不是问题:
string iotHubconnectionString = CloudConfigurationManager.GetSetting("Microsoft.IotHub.ConnectionStringPayed");
string iotHubD2cEndpoint = "messages/events";
EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(iotHubconnectionString, iotHubD2cEndpoint);
EventHubRuntimeInformation runtimeInformation = eventHubClient.GetRuntimeInformation();
然后,如果我想从客户端接收消息,请执行以下步骤:
EventHubReceiver eventHubReceiver = eventHubClient2.GetDefaultConsumerGroup().CreateReceiver(partition); //Get the partitions via the runtimeInformation: string[] partitions = runtimeInformation.PartitionIds;
var incommingMessage = eventHubReceiver.ReceiveAsync(); //Wait here for incomming messages
这很好,但是来自所有“设备”的所有消息都到达了这个“EventHubReceiver”。我想要多个接收器,只接收来自单个设备的消息。
我尝试更改以下代码行:
string iotHubD2cEndpoint = "messages/events";
至
string iotHubD2cEndpoint = "devices/{deviceID}/messages/events";
但这行不通。我收到以下错误:
The link address 'devices/secondDevice/messages/events/$management' did not match any of the expected formats. Supported formats: '/$cbs', '/devices/{deviceid}/messages/events', '/devices/{deviceid}/messages/deviceBound', '/messages/deviceBound', '/messages/serviceBound/feedback', '/messages/events/*'.
所以问题是我不知道是不是不可能为每个设备创建一个'devices/secondDevice/messages/events/$management'
EventHubReceiver'devices/secondDevice/messages/events/'
或者我的代码或思维有错误。