From 2180f59725e5673285195060e61ba3fccc895152 Mon Sep 17 00:00:00 2001 From: Cynthia MacLeod Date: Sat, 19 Aug 2023 12:48:01 +0100 Subject: [PATCH] Address issue #1818 where MqttConnectionContext throws an error when passed a TcpConnection. (#1819) * Address issue #1818 where MqttConnectionContext throws and error when passed a TcpConnection. * Update ReleaseNotes.md --------- Co-authored-by: Christian <6939810+chkr1011@users.noreply.github.com> --- .github/workflows/ReleaseNotes.md | 1 + Source/MQTTnet.AspnetCore/MqttConnectionContext.cs | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index 12c9dfebf..f9abc621b 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -3,3 +3,4 @@ * [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; }