diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index 6dd2330a..9e3a8f5f 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -12,12 +12,13 @@ true MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports v3.1.0, v3.1.1 and v5.0.0 of the MQTT protocol. -* [RcpClient] Adjusted some namespaces (BREAKING CHANGE!) -* [AspNetCore] Adjusted some namespaces (BREAKING CHANGE!) -* [Server] Adjusted some namespaces (BREAKING CHANGE!) +* [LowLevelClient] Fixed a null reference exception when connecting to a not existing server (thanks to @SGStino). +* [RcpClient] Adjusted some namespaces (BREAKING CHANGE!). +* [AspNetCore] Adjusted some namespaces (BREAKING CHANGE!). +* [Server] Adjusted some namespaces (BREAKING CHANGE!). * [Server] Added state checks (throw if not started etc.) for most server APIs. * [Server] Exposed real X509Certificate2 (instead byte array) to TLS options (thanks to @borigas). -* [Server] Fixed memory leak with TCP sockets (MqttServer is now Disposable!) (BREAKING CHANGE!) +* [Server] Fixed memory leak with TCP sockets (MqttServer is now Disposable!) (BREAKING CHANGE!). * [Core] Fixed a null reference exception in the MqttTcpChannel with WriteAsync and ReadAsync. * [Core] Added server interceptor for undelivered messages (thanks to @cshark-inator). * [nuget] Added support for SourceLink (thanks to @JTOne123). diff --git a/Source/MQTTnet/LowLevelClient/LowLevelMqttClient.cs b/Source/MQTTnet/LowLevelClient/LowLevelMqttClient.cs index c7fad070..7bb58639 100644 --- a/Source/MQTTnet/LowLevelClient/LowLevelMqttClient.cs +++ b/Source/MQTTnet/LowLevelClient/LowLevelMqttClient.cs @@ -48,7 +48,7 @@ namespace MQTTnet.LowLevelClient } catch (Exception) { - _adapter.Dispose(); + _adapter?.Dispose(); throw; } diff --git a/Tests/MQTTnet.Core.Tests/LowLevelMqttClient_Tests.cs b/Tests/MQTTnet.Core.Tests/LowLevelMqttClient_Tests.cs index 7828ea30..478df34a 100644 --- a/Tests/MQTTnet.Core.Tests/LowLevelMqttClient_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/LowLevelMqttClient_Tests.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Exceptions; namespace MQTTnet.Tests { @@ -16,6 +17,18 @@ namespace MQTTnet.Tests { public TestContext TestContext { get; set; } + [TestMethod] + [ExpectedException(typeof(MqttCommunicationException))] + public async Task Connect_To_Not_Existing_Server() + { + var client = new MqttFactory().CreateLowLevelMqttClient(); + var options = new MqttClientOptionsBuilder() + .WithTcpServer("localhost") + .Build(); + + await client.ConnectAsync(options, CancellationToken.None).ConfigureAwait(false); + } + [TestMethod] public async Task Connect_And_Disconnect() { @@ -78,7 +91,7 @@ namespace MQTTnet.Tests async Task Authenticate(ILowLevelMqttClient client) { - await client.SendAsync(new MqttConnectPacket() + await client.SendAsync(new MqttConnectPacket { CleanSession = true, ClientId = TestContext.TestName,