none
[UWP] Cloud to Device message coming as NULL RRS feed

  • Question

  • Hi,

    I am trying to send Client to device message from my web application to IoT Windows 10 device.

    Message is received successfully at the client but message body is coming as null.

    Below is the code being used at IoT client. Messages as sent by cloud is failing at the highlighted line.

                   

            private async Task ReceiveC2dAsync()
            {
                //Console.WriteLine("\nReceiving cloud to device messages from service");
                while (true)
                {
                    Message receivedMessage = await deviceClient.ReceiveAsync();
    if (receivedMessage == null) continue;

                    //Console.WriteLine("Received message: {0}", Encoding.ASCII.GetString(receivedMessage.GetBytes()));
                    await deviceClient.CompleteAsync(receivedMessage);

                    var res = await Launcher.QueryUriSupportAsync(new Uri("http://www.microsoft.com"), LaunchQuerySupportType.Uri);

                }

            }

    I am using below code in the web application (hosted in azure) to send message from cloud to device.

            private async static Task SendC2DMessage()
            {
                var commandMessage = new Message(Encoding.ASCII.GetBytes("Cloud to device message."));
                await serviceClient.SendAsync("IoTExperiment", commandMessage);
            }

    Thanks,

    /Vikas

    Sunday, January 29, 2017 6:13 AM

Answers

  • Hi Vikas,

    - to clarify your problem:

     1. The Device-To-Cloud (D2C) messaging to the Azure IoT Hub is working well.

     2. The Cloud-To-Device (C2D) messaging to the Windows IoT Core device via the Azure IoT Hub is working only from the UWP Client application (and also from the Device Explorer, isn't ?) and doesn't work from your Web App.

     You didn't show your code snippet for invoking a SendC2DMessage() method in the Web App. It should be something like this.
            

     It looks like, your problem is related with a properly implementation of the multithreading technique on the Web App (sender of the C2D message). In other words, the device side - receiver of the C2D message is OK. 

    Note, that the receivedMessage = null indicated, that the deviceClient.ReceiveAsync() exited timeout and the control is returned back to the endless loop, see my previously thread in more details.

    Thanks

    Roman



    • Edited by Roman Kiss Saturday, February 18, 2017 9:06 AM
    • Marked as answer by Vikas-Gomia Sunday, February 26, 2017 9:31 AM
    Saturday, February 18, 2017 9:01 AM

All replies

  • Hi Vikas,

    - the receivedMessage = null indicated, that the deviceClient.ReceiveAsync() exited timeout and the control is returned back to the endless loop. For TransportType.Amqp the default value of this timeout is 60 seconds. Note, that this protocol enables to setup this receiver timeout via parameter, for instance: deviceClient.ReceiveAsync(TimeSpan.FromSeconds(100)).

    The other protocol such as TransportType.Http1 has harcoded value = 0 second, in other words, the receiver for this http protocol will make a single GET request for /devices/{deviceId}/messages/deviceBound?api-version=2016-02-03 and returned back to the endless loop (polling process). If the response status = 204 (No Content), the value receivedMessage is null.

    In the other case, when the receivedMessage != null is indicated, that the receiver "peek & lock" the message from the C2D device-facing queue and the message instance is passed to its processing and used it for sending a Complete/Reject/Abandon response back. After this, the service-facing side can received a feedback message.

    I do recommend for testing purpose to use a Device Explorer with the C2D feedback monitoring to see if your device completed C2D Message sent by webapp or Device Explorer.

    Which protocol is used for your UWP Device?

    How do you know, that your UWP Device successfully received a message?

    Thanks

    Roman




    • Edited by Roman Kiss Sunday, January 29, 2017 12:15 PM
    Sunday, January 29, 2017 12:00 PM
  • Hi,

    I believe the issue is with the threading mechanism as listed here - 

    The message if sent from client UWP application reaches successfully. But if same message Itry to send it from Web App then it doesn't reaches the receiver.

    Friday, February 17, 2017 7:53 PM
  • Hi Vikas,

    - to clarify your problem:

     1. The Device-To-Cloud (D2C) messaging to the Azure IoT Hub is working well.

     2. The Cloud-To-Device (C2D) messaging to the Windows IoT Core device via the Azure IoT Hub is working only from the UWP Client application (and also from the Device Explorer, isn't ?) and doesn't work from your Web App.

     You didn't show your code snippet for invoking a SendC2DMessage() method in the Web App. It should be something like this.
            

     It looks like, your problem is related with a properly implementation of the multithreading technique on the Web App (sender of the C2D message). In other words, the device side - receiver of the C2D message is OK. 

    Note, that the receivedMessage = null indicated, that the deviceClient.ReceiveAsync() exited timeout and the control is returned back to the endless loop, see my previously thread in more details.

    Thanks

    Roman



    • Edited by Roman Kiss Saturday, February 18, 2017 9:06 AM
    • Marked as answer by Vikas-Gomia Sunday, February 26, 2017 9:31 AM
    Saturday, February 18, 2017 9:01 AM