1

I am using Azure IoT Hub. In Azure portal, it is easy to see how many cloud to device messages are pending in the queue.

I want to read this value using C# SDK

enter image description here

Jawad Sabir
  • 167
  • 2
  • 12

2 Answers2

1

I finally found the working answer as follows:

using Microsoft.Azure.Devices;

RegistryManager registryManager = RegistryManager.CreateFromConnectionString("registryreadconnectionstring");

Device device = await registryManager.GetDeviceAsync("device-id");

var count  = device.CloudToDeviceMessageCount;
Jawad Sabir
  • 167
  • 2
  • 12
-2
var device = new Microsoft.Azure.Devices.Device("device-id");
var cloudToDeviceMessageCount = device.CloudToDeviceMessageCount;
Nouman
  • 591
  • 6
  • 14
  • does not work for me, it returns unauthenticated device with default values. The count always 0. – Jawad Sabir Jul 05 '19 at 06:15
  • 1
    This answer was automatically tagged "low quality". As explained in the [help](https://stackoverflow.com/help/how-to-answer) ("Brevity is acceptable, but fuller explanations are better."), please edit your answer to tell the OP what he's doing wrong, how you reasoned and what your code is about, to show that you understood the question and to make benefit both the OP and future visitors. – Sandra Rossi Jul 05 '19 at 06:33
  • Is CloudToDeviceMessageCount only property which is returning unaunthenticated device? can you share the code how you are authenticating? – Nouman Jul 05 '19 at 07:57
  • No all properties have their default values. – Jawad Sabir Jul 05 '19 at 09:30
  • Perhaps the device is not authenticated for some reason, can your share how you are authenticating the device? – Nouman Jul 05 '19 at 09:34