protected override void Dispose()

in iothub/device/src/Transport/Mqtt/MqttTransportHandler.cs [434:493]


        protected override void Dispose(bool disposing)
        {
            try
            {
                if (Logging.IsEnabled)
                    Logging.Enter(
                        this,
                        $"{nameof(DefaultDelegatingHandler)}.Disposed={_isDisposed}; disposing={disposing}",
                        $"{nameof(MqttTransportHandler)}.{nameof(Dispose)}");

                if (!_isDisposed)
                {
                    base.Dispose(disposing);
                    if (disposing)
                    {
                        if (TryStop())
                        {
                            CleanUpAsync().GetAwaiter().GetResult();
                        }

                        // Log the task completion source tasks' exceptions and avoid unobserved exceptions.
                        if (_connectCompletion.Task.Exception != null)
                        {
                            if (Logging.IsEnabled)
                                Logging.Error(this, $"{_connectCompletion} has exception {_connectCompletion.Task.Exception}", nameof(Dispose));
                        }
                        if (_subscribeCompletionSource.Task.Exception != null)
                        {
                            if (Logging.IsEnabled)
                                Logging.Error(this, $"{_subscribeCompletionSource} has exception {_subscribeCompletionSource.Task.Exception}", nameof(Dispose));
                        }

                        _disconnectAwaitersCancellationSource?.Dispose();
                        _disconnectAwaitersCancellationSource = null;

                        _receivingSemaphore?.Dispose();
                        _receivingSemaphore = null;

                        _deviceReceiveMessageSemaphore?.Dispose();
                        _deviceReceiveMessageSemaphore = null;

                        if (_channel is IDisposable disposableChannel)
                        {
                            disposableChannel.Dispose();
                            _channel = null;
                        }
                    }

                    // the _disposed flag is inherited from the base class DefaultDelegatingHandler and is finally set to null there.
                }
            }
            finally
            {
                if (Logging.IsEnabled)
                    Logging.Exit(
                        this,
                        $"{nameof(DefaultDelegatingHandler)}.Disposed={_isDisposed}; disposing={disposing}",
                        $"{nameof(MqttTransportHandler)}.{nameof(Dispose)}");
            }
        }