0

I am using the MQTTNet library for the MQTT connection in my application. I am using Mosquitto Broker as a MQTT broker. My app is in .Net core 3.1.

I am having requirement to send the MQTT message to device when the app is connected, normal disconnected and unexpected disconnected.

For the connected scenario, I am using the UseConnectedHandler extension method of the IMQTTClient.

mqttClient.UseConnectedHandler((MqttClientConnectedEventArgs e) =>
         {
            // Console.WriteLine("MqttClient - Connected");
            // Publish the Connect Message
         });

For the unexpected disconnect scenario I am using the WithWillMessage feature.

var mqttOptions = new ManagedMqttClientOptionsBuilder()
            .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
            .WithClientOptions(new MqttClientOptionsBuilder()
            .WithWillMessage(_options.LastWillMessage)
               .WithCleanSession()
               .WithClientId(_options.DevcommMqttClientId)
               .WithProtocolVersion(MqttProtocolVersion.V500)
               .WithCommunicationTimeout(TimeSpan.FromSeconds(30))
               .WithCredentials(_options.DevcommMqttUsername ?? string.Empty, _options.DevcommMqttPassword ?? string.Empty)
               .WithTcpServer(_options.DevcommMqttHost, _options.DevcommMqttPort)
               .WithTls(o =>
               {
                  o.UseTls = _options.DevcommUseTls;
                  o.AllowUntrustedCertificates = _options.DevcommTlsAllowUntrustedCertificates;
                  o.IgnoreCertificateChainErrors = _options.DevcommTlsIgnoreCertificateChainErrors;
                  o.IgnoreCertificateRevocationErrors = _options.DevcommTlsIgnoreCertificateRevocationErrors;
                  o.SslProtocol = ParseSslProtocols(_options.DevcommTlsProtocols);
                  o.Certificates = _options.DevcommTlsClientCertificates.AsEnumerable();
                  o.CertificateValidationHandler = CertificateValidationHandler;
               })
               .Build())
            .Build();

I am now stuck at graceful disconnect scenario. The UseDisconnectedHandler extension method is being called after the disconnect and not before disconnect.

Mosquitto broker is providing the feature of close message, which is similar of before disconnect event.

Is there anyway that using MQTTNet I can send the before disconnect message?

John
  • 351
  • 4
  • 16
  • You will need to explicitly send the message before calling disconnect. – hardillb Feb 09 '22 at 09:15
  • @hardillb The MQTTNet implementation is in another common library and my console app is consuming the same. I am not getting any before close event. `IHostApplicationLifeCycle`'s `StopAsync` event is not useful in this scenario. – John Feb 09 '22 at 09:48
  • But you must know where you are calling disconnect for it to be a clean shutdown? – hardillb Feb 09 '22 at 09:52
  • The problem is, Common library is having the Extension method which hosts the MQTTHost BackgroundService class. I am using the extension method in my console app and not having control of that MQTTHost's StopAsync method. When I stop my application the close sequence is 1. MQTTHost-> 2. StopAsync() -> 3. Sends the MQTTNet Disconnect-> 4. MQTT DisconnectedHandler ->5. Console's StopAsync. Unfortunately I am not having control over steps 1 to 3. In this scenario on before disconnect handler is useful. – John Feb 09 '22 at 10:10

0 Answers0