From eb25500f508751365d18ca1e0d7d1b911b1d1c66 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Fri, 8 Dec 2023 18:01:19 +0100 Subject: [PATCH] 1889 no service for type mqttnetservermqttserver has been registered (#1892) * Add missing service to service collection * Update ReleaseNotes.md * Restore service registrations --- .github/workflows/ReleaseNotes.md | 1 + Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs | 4 +++- Source/MQTTnet/Adapter/IMqttServerAdapter.cs | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index 61707d850..e04b0b0d2 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1,2 +1,3 @@ * [Server] Added new events for delivered and dropped messages (#1866, thanks to @kallayj). * [Server] The server will no longer treat a client which is receiving a large payload as alive. The packet must be received completely within the keep alive boundaries (BREAKING CHANGE!, #1883). +* [Server] Fixed "service not registered" exception in ASP.NET integration (#1889). diff --git a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs index 9b46ad19f..1186baeb7 100644 --- a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs +++ b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs @@ -5,6 +5,7 @@ using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Hosting; using MQTTnet.Adapter; using MQTTnet.Diagnostics; using MQTTnet.Implementations; @@ -58,7 +59,8 @@ public static void AddHostedMqttServer(this IServiceCollection services) services.TryAddSingleton(new MqttFactory()); services.AddSingleton(); - services.AddHostedService(); + services.AddSingleton(s => s.GetService()); + services.AddSingleton(s => s.GetService()); } public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action configure) diff --git a/Source/MQTTnet/Adapter/IMqttServerAdapter.cs b/Source/MQTTnet/Adapter/IMqttServerAdapter.cs index 328430521..47f3c1d37 100644 --- a/Source/MQTTnet/Adapter/IMqttServerAdapter.cs +++ b/Source/MQTTnet/Adapter/IMqttServerAdapter.cs @@ -14,6 +14,7 @@ public interface IMqttServerAdapter : IDisposable Func ClientHandler { get; set; } Task StartAsync(MqttServerOptions options, IMqttNetLogger logger); + Task StopAsync(); } }