如果没有处理程序,Azure IoT 中心直接方法调用会发生什么情况

What happens to Azure IoT Hub Direct Method invocation if there is no handler

我将 Azure IoT 中心与 C# 桌面应用程序客户端一起使用。我在服务器上引入了一个新的直接方法调用,并且还向新的客户端版本添加了一个处理程序。 但是我想知道如果我将更改发布到后端,那些没有此直接方法处理程序的旧客户端会发生什么情况。

这会导致异常并导致崩溃,还是调用会被忽略? 我尝试在 SO 和其他地方搜索此问题的答案,但找不到任何答案。

查看文档:Handle a direct method on device.

更新:

以下案例显示了使用 REST POST 调用设备直接方法的所有响应示例:

https://xxxxxxxx.azure-devices.net/twins/myDevice6/methods?api-version=2018-06-30

有效载荷:

{
  "methodName": "writeLine",
  "responseTimeoutInSeconds": 10,
  "payload": {
    "input1": 12345,
    "input2": "HelloDevice"
    }
}

响应1:设备未连接

{
  "Message": "{\"errorCode\":404103,\"trackingId\":\"e796f79f02094184ba375533dd522b62-TimeStamp:06/18/2019 06:08:11\",\"message\":\"Timed out waiting for device to connect.\",\"info\":{\"timeout\":\"00:00:00\"},\"timestampUtc\":\"2019-06-18T06:08:11.1216684Z\"}",
  "ExceptionMessage": ""
}

响应 2:设备已连接但未订阅

{
  "Message": "{\"errorCode\":404103,\"trackingId\":\"4d2c65b6fd994fc5adc902ecfade1877-G:3-TimeStamp:06/18/2019 06:11:52-G:11-TimeStamp:06/18/2019 06:11:52\",\"message\":\"Timed out waiting for device to subscribe.\",\"info\":{},\"timestampUtc\":\"2019-06-18T06:11:52.6039066Z\"}",
  "ExceptionMessage": ""
}

响应 3:设备订阅了另一个方法名称,例如$iothub/methods/POST/xyz/#

{
  "Message": "{\"errorCode\":404103,\"trackingId\":\"59df13ecc3a04d63b7a1813ed9e6187f-G:3-TimeStamp:06/18/2019 06:14:48-G:10-TimeStamp:06/18/2019 06:14:48\",\"message\":\"Timed out waiting for device to subscribe.\",\"info\":{},\"timestampUtc\":\"2019-06-18T06:14:48.110565Z\"}",
  "ExceptionMessage": ""
}

响应 4:设备已订阅特定方法名称但没有响应,例如$iothub/methods/POST/writeLine/#

{
  "Message": "{\"errorCode\":504101,\"trackingId\":\"1692ee301e344215945385b282fd0b78-G:3-TimeStamp:06/18/2019 06:16:48-G:12-TimeStamp:06/18/2019 06:16:48\",\"message\":\"Timed out waiting for the response from device.\",\"info\":{},\"timestampUtc\":\"2019-06-18T06:16:48.628664Z\"}",
  "ExceptionMessage": ""
}

响应 5:设备已订阅任何方法名称,但没有响应,例如$iothub/methods/POST/#

{
  "Message": "{\"errorCode\":504101,\"trackingId\":\"7edc4ecfc095424c9a378a2e064bc886-G:3-TimeStamp:06/18/2019 06:59:24-G:16-TimeStamp:06/18/2019 06:59:24\",\"message\":\"Timed out waiting for the response from device.\",\"info\":{},\"timestampUtc\":\"2019-06-18T06:59:24.0184557Z\"}",
  "ExceptionMessage": ""
}

基本上,您的案例可以是 Response 3. 或 Response 5.

可能您的客户端应用程序正在使用 Azure IoT C# SDK,其中 MQTTTransportHandler class 有一个 属性 methodPostTopicFilter = " $iothub/methods/POST/#",因此您的响应是 #5 应该带有 errorCode = 504,但 device SDK 正在生成 errorCode = 501 作为对未实现处理程序的响应(发布主题 $iothub/methods/res/501/?$rid={requestId})。

请注意,响应时间由调用者定义,请参阅有效负载正文中的 属性 responseTimeoutInSeconds。在响应 3 的情况下,响应时间约为 5 秒。

它会被忽略,并且 return 给调用者一个 501 错误。