From 09718a00e731d64ef73a54bf6efc3dec130e584f Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 23 Feb 2019 10:13:12 +0100 Subject: [PATCH] Add handlers to variable before using to avoid NullRefExceptions. --- Source/MQTTnet/Client/MqttClient.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 3e92a28..d431ba5 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -100,12 +100,14 @@ namespace MQTTnet.Client _logger.Info("Connected."); - Connected?.Invoke(this, new MqttClientConnectedEventArgs(authenticateResult)); - if(ConnectedHandler != null) + var connectedHandler = ConnectedHandler; + if (connectedHandler != null) { - await ConnectedHandler.HandleConnectedAsync(new MqttClientConnectedEventArgs(authenticateResult)); + await connectedHandler.HandleConnectedAsync(new MqttClientConnectedEventArgs(authenticateResult)).ConfigureAwait(false); } - + + // TODO: Remove! + Connected?.Invoke(this, new MqttClientConnectedEventArgs(authenticateResult)); return authenticateResult; } @@ -268,11 +270,14 @@ namespace MQTTnet.Client _cleanDisconnectInitiated = false; _logger.Info("Disconnected."); - Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected, exception)); - if(DisconnectedHandler != null) + + var disconnectedHandler = DisconnectedHandler; + if (disconnectedHandler != null) { - await DisconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception)); + await disconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception)).ConfigureAwait(false); } + + Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected, exception)); } }