Skip to content

Commit dfc09e3

Browse files
committed
MqttHostedServer based on BackgroundService
1 parent b2cb8c9 commit dfc09e3

File tree

2 files changed

+13
-41
lines changed

2 files changed

+13
-41
lines changed

Source/MQTTnet.AspnetCore/MqttHostedServer.cs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,33 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Microsoft.Extensions.Hosting;
10-
using Microsoft.Extensions.Logging;
11-
using Microsoft.Extensions.Logging.Abstractions;
1210
using MQTTnet.Diagnostics.Logger;
1311
using MQTTnet.Server;
1412

1513
namespace MQTTnet.AspNetCore;
1614

17-
public sealed class MqttHostedServer : MqttServer, IHostedService
15+
public sealed class MqttHostedServer : BackgroundService
1816
{
19-
readonly IHostApplicationLifetime _hostApplicationLifetime;
2017
readonly MqttServerFactory _mqttFactory;
21-
readonly ILogger _appLogger;
22-
2318
public MqttHostedServer(
24-
IHostApplicationLifetime hostApplicationLifetime,
2519
MqttServerFactory mqttFactory,
2620
MqttServerOptions options,
2721
IEnumerable<IMqttServerAdapter> adapters,
28-
IMqttNetLogger logger,
29-
ILogger<MqttHostedServer> appLogger = null
30-
) : base(options, adapters, logger)
22+
IMqttNetLogger logger
23+
)
3124
{
25+
MqttServer = new(options, adapters, logger);
3226
_mqttFactory = mqttFactory ?? throw new ArgumentNullException(nameof(mqttFactory));
33-
_hostApplicationLifetime = hostApplicationLifetime;
34-
_appLogger = appLogger ?? NullLogger<MqttHostedServer>.Instance;
35-
}
36-
37-
public async Task StartAsync(CancellationToken cancellationToken)
38-
{
39-
// The yield makes sure that the hosted service is considered up and running.
40-
await Task.Yield();
41-
42-
_hostApplicationLifetime.ApplicationStarted.Register(OnStarted);
4327
}
4428

45-
public Task StopAsync(CancellationToken cancellationToken)
29+
public MqttServer MqttServer { get; }
30+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
4631
{
47-
return StopAsync(_mqttFactory.CreateMqttServerStopOptionsBuilder().Build());
32+
await MqttServer.StartAsync();
4833
}
49-
50-
void OnStarted()
34+
public override async Task StopAsync(CancellationToken cancellationToken)
5135
{
52-
async Task DoStart()
53-
{
54-
try
55-
{
56-
await StartAsync();
57-
}
58-
catch (Exception e)
59-
{
60-
_appLogger.LogError(e, "Stopping application: failed to start MqttServer: {Error}", e.Message);
61-
_hostApplicationLifetime.StopApplication();
62-
throw;
63-
}
64-
}
65-
_ = DoStart();
36+
await MqttServer.StopAsync(_mqttFactory.CreateMqttServerStopOptionsBuilder().Build());
37+
await base.StopAsync(cancellationToken);
6638
}
67-
}
39+
}

Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public static void AddHostedMqttServer(this IServiceCollection services)
4545
services.TryAddSingleton(new MqttServerFactory());
4646

4747
services.AddSingleton<MqttHostedServer>();
48-
services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
49-
services.AddSingleton<MqttServer>(s => s.GetService<MqttHostedServer>());
48+
services.AddHostedService(s => s.GetService<MqttHostedServer>());
49+
services.AddSingleton<MqttServer>(s => s.GetService<MqttHostedServer>().MqttServer);
5050
}
5151

5252
public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure)

0 commit comments

Comments
 (0)