diff --git a/nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj b/nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj index a19385f..cf1c9eb 100644 --- a/nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj +++ b/nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj @@ -69,15 +69,12 @@ ..\packages\nanoFramework.Json.2.2.101\lib\nanoFramework.Json.dll - True ..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.dll - True ..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.Core.dll - True ..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll diff --git a/nanoFramework.Azure.Devices.Client/DeviceClient.cs b/nanoFramework.Azure.Devices.Client/DeviceClient.cs index 61d8553..2861b31 100644 --- a/nanoFramework.Azure.Devices.Client/DeviceClient.cs +++ b/nanoFramework.Azure.Devices.Client/DeviceClient.cs @@ -50,6 +50,8 @@ public class DeviceClient : IDisposable private readonly object _lock = new object(); private Timer _timerTokenRenew; private bool _hasClientCertificate; + private bool _isDisposed = false; + private bool _isClosed = true; /// /// Device twin updated event. @@ -359,6 +361,7 @@ public bool Open() _timerTokenRenew = new Timer(TimerCallbackReconnect, null, new TimeSpan(23, 50, 0), TimeSpan.MaxValue); } + _isClosed = !_mqttc.IsConnected; return _mqttc.IsConnected; } @@ -382,6 +385,11 @@ private void TimerCallbackReconnect(object state) /// public void Close() { + if (_isClosed) + { + return; + } + if (_mqttc != null) { if (_mqttc.IsConnected) @@ -394,13 +402,21 @@ public void Close() } _mqttc.Disconnect(); - _mqttc.Close(); + try + { + _mqttc.Close(); + } + catch + { + // Nothing on purpose, we want to make sure we can continue + } // Make sure all get disconnected, cleared Thread.Sleep(1000); } _timerTokenRenew?.Dispose(); + _isClosed = true; } /// @@ -804,6 +820,12 @@ private void ClientConnectionClosed(object sender, EventArgs e) /// public void Dispose() { + if (_isDisposed) + { + return; + } + + _isDisposed = true; if (_mqttc != null) { // Making sure we unregister the registered events @@ -815,7 +837,6 @@ public void Dispose() while (_mqttc.IsConnected) { Thread.Sleep(100); - } // Cleaning diff --git a/nanoFramework.Azure.Devices.Client/ProvisioningDeviceClient.cs b/nanoFramework.Azure.Devices.Client/ProvisioningDeviceClient.cs index 090ebc0..6fd14e0 100644 --- a/nanoFramework.Azure.Devices.Client/ProvisioningDeviceClient.cs +++ b/nanoFramework.Azure.Devices.Client/ProvisioningDeviceClient.cs @@ -34,6 +34,7 @@ public class ProvisioningDeviceClient : IDisposable private ProvisioningRegistrationStatusType _status = ProvisioningRegistrationStatusType.Unassigned; private DeviceRegistrationResult _result = null; private bool _isMessageProcessed = false; + private bool _isDisposed = false; /// /// Creates an instance of the Device Provisioning Client. @@ -228,9 +229,17 @@ private void CleanAll() { // We need to clean everything now _mqttc.MqttMsgPublishReceived -= ClientMqttMsgReceived; - _mqttc.Unsubscribe(new[] { + try + { + _mqttc.Unsubscribe(new[] { DpsSubscription }); + } + catch + { + // Nothing on purpose, just cleaning + } + _mqttc.Disconnect(); while (_mqttc.IsConnected) { @@ -337,6 +346,12 @@ private ProvisioningRegistrationStatusType AssignStatus(string status) /// public void Dispose() { + if(_isDisposed) + { + return; + } + + _isDisposed = true; if (_mqttc != null) { CleanAll();