/// <summary>
        /// Opens the MQTT connection.
        /// </summary>
        public async Task StartAsync(IProcesMqttMessage messageHandler)
        {
            try
            {
                lock (startupLock)
                {
                    if (_managedMqttClient != null || ConnectionState == ConnectionState.Connected)
                    {
                        return;
                    }
                    _messageHandler = messageHandler;

                    ConnectionState    = ConnectionState.Connecting;
                    _managedMqttClient = _mqttClientFactory.CreateManagedMqttClient();
                    _managedMqttClient.ApplicationMessageReceivedHandler  = this;
                    _managedMqttClient.ApplicationMessageProcessedHandler = this;
                    _managedMqttClient.ConnectedHandler        = this;
                    _managedMqttClient.ConnectingFailedHandler = this;
                    _managedMqttClient.SynchronizingSubscriptionsFailedHandler = this;
                    _managedMqttClient.DisconnectedHandler = this;
                }
                await _managedMqttClient.StartAsync(_config.Options).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                _logger.LogCritical(new EventId(0), e, $"Exception while setting up the mqttclient to {this}");
                throw new MqttConnectionException($"Exception while setting up the mqttclient to {this}", e);
            }
        }
        public async Task AddAsync(IMqttMessage item, CancellationToken cancellationToken = default)
        {
            if (_mqttConnection.ConnectionState != ConnectionState.Connected)
            {
                IProcesMqttMessage messageProcessor = null; // this is only for publising, we dont expect incoming messages
                await _mqttConnection.StartAsync(messageProcessor).ConfigureAwait(false);

                for (var i = 0; i < 100; i++)
                {
                    if (_mqttConnection.ConnectionState != ConnectionState.Connected)
                    {
                        await Task.Delay(50).ConfigureAwait(false);
                    }
                }
            }
            var qos = (MQTTnet.Protocol.MqttQualityOfServiceLevel)Enum.Parse(typeof(MQTTnet.Protocol.MqttQualityOfServiceLevel), item.QosLevel.ToString());
            var mqttApplicationMessage = new MqttApplicationMessage
            {
                Topic   = item.Topic,
                Payload = item.GetMessage(),
                QualityOfServiceLevel = qos,
                Retain = item.Retain
            };
            await _mqttConnection.PublishAsync(mqttApplicationMessage).ConfigureAwait(false);
        }
        /// <summary>
        /// Close the MQTT connection.
        /// </summary>
        public Task StopAsync()
        {
            if (_managedMqttClient == null)
            {
                return(Task.CompletedTask);
            }

            ConnectionState = ConnectionState.Disconnected;

            _managedMqttClient.StopAsync().Wait();
            _managedMqttClient.Dispose();
            _managedMqttClient = null;

            _messageHandler = null;

            return(Task.CompletedTask);
        }