【问题标题】:Get a list of devices from Azure IoT Hub从 Azure IoT 中心获取设备列表
【发布时间】:2017-06-15 09:17:17
【问题描述】:

我是 Microsoft Azure IoT Hub 的新手。我想获取我的 IoT Hub 中的设备列表并检查列表中是否有任何设备。

如果我使用控制台应用程序,它可以正常工作。

static async void QueryDevices()
{
  registryManager = RegistryManager.CreateFromConnectionString(DeviceConnectionString);

  var devices = await registryManager.GetDevicesAsync(100); // Time 1 sek

  foreach (var item in devices)
  {
    Console.WriteLine("Divice id: " + item.Id + ", Connection state: " + item.ConnectionState);
  }
  Console.WriteLine("\nNumber of devices: " + devices.Count());

}

但是,如果我在 WebAPI 中使用“相同”代码,GetDevicesAsync() 会继续运行并且没有任何结果。

public bool CheckIoTHubConnection(string iotHubConnectionString)
{
  registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString);

  return CreateConnection().Result;
}

private async Task<bool> CreateConnection()
{
  bool connectionOk = false;

  try
  {
    // Gets all devices from IoT Hub
    var result = await registryManager.GetDevicesAsync(10); // This never gets further

    if (result.Any())
      connectionOk = true;
  }
  catch (Exception ex)
  {
    connectionOk = false;
    throw ex;
  }

  return connectionOk;
}

我做错了什么?

【问题讨论】:

  • 现在这个方法已经过时了,没有替代品......

标签: c# azure asp.net-web-api azure-iot-hub


【解决方案1】:

你可以试试这个代码格式:

...

System.Threading.ThreadPool.QueueUserWorkItem(a => CheckIoTHubConnection(iotHubConnStr));

...

它对我有用。

更多信息可以参考初始帖子“Send to IoT hub from MVC Web API?”。

而对于这个问题,@Jason Malinowski's answer可能会在一定程度上解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 2019-04-27
    • 2023-03-17
    • 1970-01-01
    • 2023-01-27
    • 2015-12-27
    相关资源
    最近更新 更多