只要设备正在运行,我就需要运行一个保持活动功能。该方法位于模块内部。我遇到3-10小时后停止运行的情况。

// Async method to send keepalive signals
private static async void SendKeepaliveToCloudMessagesAsync()
{
    int keep_alive_counter = 0;
    while (true)
    {
        try
        {
            String timestamp = DateTimeOffset.UtcNow.ToString("u");
            String activity_type = "Device-Keepalive";
            // Create JSON message
            var telemetryDataPoint = new
            {
                timestamp,
                activity_type,
                device_id,
                keep_alive_counter
            };
            var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
            var message = new Message(Encoding.ASCII.GetBytes(messageString));
            keep_alive_counter++;

            // Add a custom application property to the message.
            // An IoT hub can filter on these properties without access to the message body.
            message.Properties.Add("keepaliveAlert", (keep_alive_counter < 30) ? "true" : "false");

            // Send the telemetry message
            await s_deviceClient.SendEventAsync(message);
            Console.WriteLine("[{0}] > Sending Keepalive message: {1}", DateTimeOffset.UtcNow.ToString("u"), messageString);

            await Task.Delay(s_keepaliveInterval * 1000);
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine("Send keepalive Failed! {0}", ex);
        }
    }
}


上面的代码可以正常工作3到10个小时,但随后突然停止,并且我没有在IoTHub上收到保持活动消息。

我设法从日志中获取以下消息:

Send keepalive Failed! System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IoT Client'.
   at Microsoft.Azure.Devices.Client.Transport.DefaultDelegatingHandler.ThrowIfDisposed()
   at Microsoft.Azure.Devices.Client.Transport.DefaultDelegatingHandler.OpenAsync(CancellationToken cancellationToken)
   at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.<>c__DisplayClass32_0.<<OpenAsyncInternal>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.EnsureOpenedAsync(CancellationToken cancellationToken)
   at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.<>c__DisplayClass14_0.<<SendEventAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.SendEventAsync(Message message, CancellationToken cancellationToken)
   at Microsoft.Azure.Devices.Client.InternalClient.SendEventAsync(Message message)
   at MotionDetection.Program.SendKeepaliveToCloudMessagesAsync() in /app/Program.cs:line 439

最佳答案

虽然您看到的错误应该不会发生,但我仍然可以回答实际的问题:

您构造模块以无限制时间发送消息的方式看起来不错,并且通常应该可以正常工作。实际上,官方报告中的IoT Edge团队样本之一非常像您,请参阅here(对于SendUnlimitedMessages()true的情况)。

也许只是按照他们的示例,并为干净的退出策略实现关闭处理程序即可。

关于c# - 在Azure IoTEdge中的模块中启动长期运行的方法的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57233072/

10-14 11:18
查看更多