1

我在接收 Mqtt 协议上的消息属性和消息系统属性时遇到问题。但是在使用 TransportType.Amqp 时效果很好。

是否可以在此页面的“接收消息”部分找到说明? https://azure.microsoft.com/da-dk/documentation/articles/iot-hub-mqtt-support/ 如果是,这是什么意思,我该怎么做?

接收方代码:我正在使用来自 nuget 和所有依赖项的 Microsoft.Azure.Devices.Client 的 1.0.12。

//connectString: HostName=<host name>;DeviceId=<device id>;SharedAccessKey=<key>
//var deviceClient = DeviceClient.CreateFromConnectionString(connectString,TransportType.Amqp);
var deviceClient = DeviceClient.CreateFromConnectionString(connectString, TransportType.Mqtt);
while (true)
{
    var message = await deviceClient.ReceiveAsync(TimeSpan.FromSeconds(2));
    if (message != null)
    {
        //message.MessageId is null on Mqtt but not for Amqp!
    }
}

发件人代码:我使用的是 Microsoft.Azure.Devices.dll 的 1.0.11 版

string messageId = Guid.NewGuid().ToString("D");
var iotHubConnection = "HostName=<host>;SharedAccessKeyName=<...>;SharedAccessKey=<...>";
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnection);
if (serviceClient == null) throw new Exception("ServiceClient create failed");

var serviceMessage = new Microsoft.Azure.Devices.Message(Encoding.UTF8.GetBytes("Hello World"));
serviceMessage.Ack = DeliveryAcknowledgement.Full;
serviceMessage.MessageId = messageId;
serviceMessage.Properties["message-id"] = messageId;

serviceClient.SendAsync("0123", serviceMessage).Wait();

C 示例代码读取属性,但 csharp 示例代码不读取: https ://github.com/Azure/azure-iot-sdks/blob/master/c/iothub_client/samples/iothub_client_sample_mqtt/iothub_client_sample_mqtt.c

4

0 回答 0