Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix ignored exceptions in MqttHostedServer startup #2102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions Source/MQTTnet.AspnetCore/MqttHostedServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,26 @@

namespace MQTTnet.AspNetCore;

public sealed class MqttHostedServer : MqttServer, IHostedService
public sealed class MqttHostedServer : BackgroundService
{
readonly IHostApplicationLifetime _hostApplicationLifetime;
readonly MqttServerFactory _mqttFactory;

public MqttHostedServer(
IHostApplicationLifetime hostApplicationLifetime,
MqttServerFactory mqttFactory,
MqttServerOptions options,
IEnumerable<IMqttServerAdapter> adapters,
IMqttNetLogger logger) : base(options, adapters, logger)
IMqttNetLogger logger
)
{
MqttServer = new(options, adapters, logger);
_mqttFactory = mqttFactory ?? throw new ArgumentNullException(nameof(mqttFactory));
_hostApplicationLifetime = hostApplicationLifetime;
}

public async Task StartAsync(CancellationToken cancellationToken)
{
// The yield makes sure that the hosted service is considered up and running.
await Task.Yield();

_hostApplicationLifetime.ApplicationStarted.Register(OnStarted);
}

public Task StopAsync(CancellationToken cancellationToken)
{
return StopAsync(_mqttFactory.CreateMqttServerStopOptionsBuilder().Build());
}

void OnStarted()
public MqttServer MqttServer { get; }
protected override Task ExecuteAsync(CancellationToken stoppingToken)
=> MqttServer.StartAsync();
public override async Task StopAsync(CancellationToken cancellationToken)
{
_ = StartAsync();
await MqttServer.StopAsync(_mqttFactory.CreateMqttServerStopOptionsBuilder().Build());
await base.StopAsync(cancellationToken);
}
}
}
4 changes: 2 additions & 2 deletions Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static void AddHostedMqttServer(this IServiceCollection services)
services.TryAddSingleton(new MqttServerFactory());

services.AddSingleton<MqttHostedServer>();
services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
services.AddSingleton<MqttServer>(s => s.GetService<MqttHostedServer>());
services.AddHostedService(s => s.GetService<MqttHostedServer>());
services.AddSingleton<MqttServer>(s => s.GetService<MqttHostedServer>().MqttServer);
}

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