diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index 29162130a..2038f9d34 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1,6 +1,9 @@ * [Client] Fixed _PlatformNotSupportedException_ when using Blazor (#1755, thanks to @Nickztar). * [Client] Added hot reload of client certificates (#1781). * [Client] Added several new option builders and aligned usage (#1781, BREAKING CHANGE!). +* [Client] Fixed wrong logging of obsolete feature when connection was not successful (#1801, thanks to @ramonsmits). +* [Client] Fixed _NullReferenceException_ when performing several actions when not connected (#1800, thanks to @ramonsmits). * [Server] Fixed _NullReferenceException_ in retained messages management (#1762, thanks to @logicaloud). * [Server] Exposed new option which allows disabling packet fragmentation (#1753). * [Server] Expired sessions will no longer be used when a client connects (#1756). +* [Server] Fixed an issue in connection handling for ASP.NET connections (#1819, thanks to @CZEMacLeod). diff --git a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs index 5804d8569..46d91412a 100644 --- a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs +++ b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs @@ -33,8 +33,11 @@ public MqttConnectionContext(MqttPacketFormatterAdapter packetFormatterAdapter, PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter)); _connection = connection ?? throw new ArgumentNullException(nameof(connection)); - _input = connection.Transport.Input; - _output = connection.Transport.Output; + if (!(_connection is TcpConnection tcp) || tcp.IsConnected) + { + _input = connection.Transport.Input; + _output = connection.Transport.Output; + } } public long BytesReceived { get; private set; } diff --git a/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Connection_Tests.cs b/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Connection_Tests.cs index d38fc6926..996f71ebc 100644 --- a/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Connection_Tests.cs +++ b/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Connection_Tests.cs @@ -196,5 +196,27 @@ public async Task Return_Non_Success() Assert.AreEqual(response.UserProperties[0].Value, "Value"); } } + + [TestMethod] + public async Task Throw_Proper_Exception_When_Not_Connected() + { + try + { + var mqttFactory = new MqttFactory(); + using (var mqttClient = mqttFactory.CreateMqttClient()) + { + await mqttClient.SubscribeAsync("test", MqttQualityOfServiceLevel.AtLeastOnce); + } + } + catch (MqttCommunicationException exception) + { + if (exception.Message == "The client is not connected.") + { + return; + } + } + + Assert.Fail(); + } } } \ No newline at end of file diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 2b851ea69..8556484e2 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -321,14 +321,14 @@ public async Task SubscribeAsync(MqttClientSubscribeO MqttTopicValidator.ThrowIfInvalidSubscribe(topicFilter.Topic); } + ThrowIfDisposed(); + ThrowIfNotConnected(); + if (Options.ValidateFeatures) { MqttClientSubscribeOptionsValidator.ThrowIfNotSupported(options, _adapter.PacketFormatterAdapter.ProtocolVersion); } - ThrowIfDisposed(); - ThrowIfNotConnected(); - var subscribePacket = MqttPacketFactories.Subscribe.Create(options); subscribePacket.PacketIdentifier = _packetIdentifierProvider.GetNextPacketIdentifier(); @@ -465,11 +465,10 @@ async Task Authenticate(IMqttChannelAdapter channelAdap // did send a proper ACK packet with a non success response. if (options.ThrowOnNonSuccessfulConnectResponse) { - _logger.Warning( - "Client will now throw an _MqttConnectingFailedException_. This is obsolete and will be removed in the future. Consider setting _ThrowOnNonSuccessfulResponseFromServer=False_ in client options."); - if (result.ResultCode != MqttClientConnectResultCode.Success) { + _logger.Warning( + "Client will now throw an _MqttConnectingFailedException_. This is obsolete and will be removed in the future. Consider setting _ThrowOnNonSuccessfulResponseFromServer=False_ in client options."); throw new MqttConnectingFailedException($"Connecting with MQTT server failed ({result.ResultCode}).", null, result); } } @@ -1060,4 +1059,4 @@ async Task TrySendKeepAliveMessages(CancellationToken cancellationToken) } } } -} \ No newline at end of file +} diff --git a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs index e7e0f7016..a1ba5f480 100644 --- a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs @@ -521,6 +521,12 @@ public MqttClientOptionsBuilder WithWillRetain(bool willRetain = true) return this; } + public MqttClientOptionsBuilder WithWillMessageExpiryInterval(uint willMessageExpiryInterval) + { + _options.WillMessageExpiryInterval = willMessageExpiryInterval; + return this; + } + public MqttClientOptionsBuilder WithWillTopic(string willTopic) { _options.WillTopic = willTopic;