Skip to content

Commit

Permalink
Improve ASP.NET integration
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Nov 15, 2023
1 parent bad1da7 commit 306c5df
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 48 deletions.
12 changes: 6 additions & 6 deletions Source/MQTTnet.AspnetCore/MqttHostedServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
using MQTTnet.Adapter;
using MQTTnet.Diagnostics;
using MQTTnet.Server;
using MQTTnet.Server.Disconnecting;

namespace MQTTnet.AspNetCore
{
public sealed class MqttHostedServer : MqttServer, IHostedService
{
public MqttHostedServer(MqttServerOptions options, IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger)
: base(options, adapters, logger)
public MqttHostedServer(MqttServerOptions options, IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger) : base(options, adapters, logger)
{
}

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

await StartAsync();
}

public Task StopAsync(CancellationToken cancellationToken)
Expand Down
108 changes: 66 additions & 42 deletions Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection.Extensions;
using MQTTnet.Adapter;
using MQTTnet.Diagnostics;
using MQTTnet.Implementations;
Expand All @@ -14,76 +14,100 @@ namespace MQTTnet.AspNetCore
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMqttServer(this IServiceCollection serviceCollection, Action<MqttServerOptionsBuilder> configure = null)
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, MqttServerOptions options)
{
if (serviceCollection is null)
if (services == null)
{
throw new ArgumentNullException(nameof(serviceCollection));
throw new ArgumentNullException(nameof(services));
}

serviceCollection.AddMqttConnectionHandler();
serviceCollection.AddHostedMqttServer(configure);

return serviceCollection;
}

public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, MqttServerOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

services.AddSingleton(options);

services.AddHostedMqttServer();

return services;
}

public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action<MqttServerOptionsBuilder> configure = null)
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action<MqttServerOptionsBuilder> configure)
{
services.AddSingleton(s =>
if (services == null)
{
var serverOptionsBuilder = new MqttServerOptionsBuilder();
configure?.Invoke(serverOptionsBuilder);
return serverOptionsBuilder.Build();
});
throw new ArgumentNullException(nameof(services));
}

services.AddHostedMqttServer();
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

return services;
var serverOptionsBuilder = new MqttServerOptionsBuilder();
configure.Invoke(serverOptionsBuilder);
var options = serverOptionsBuilder.Build();

return AddHostedMqttServer(services, options);
}

public static void AddHostedMqttServer(this IServiceCollection services)
{
services.TryAddSingleton<IMqttNetLogger>(MqttNetNullLogger.Instance);

services.AddSingleton<MqttHostedServer>();
services.AddHostedService<MqttHostedServer>();
}

public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure)
{
services.AddSingleton(s =>
if (services == null)
{
var builder = new AspNetMqttServerOptionsBuilder(s);
configure(builder);
return builder.Build();
});
throw new ArgumentNullException(nameof(services));
}

services.AddSingleton(
s =>
{
var builder = new AspNetMqttServerOptionsBuilder(s);
configure(builder);
return builder.Build();
});

services.AddHostedMqttServer();

return services;
}

static IServiceCollection AddHostedMqttServer(this IServiceCollection services)
public static IServiceCollection AddMqttConnectionHandler(this IServiceCollection services)
{
var logger = new MqttNetEventLogger();

services.AddSingleton<IMqttNetLogger>(logger);
services.AddSingleton<MqttHostedServer>();
services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
services.AddSingleton<MqttServer>(s => s.GetService<MqttHostedServer>());
services.AddSingleton<MqttConnectionHandler>();
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttConnectionHandler>());

return services;
}

public static IServiceCollection AddMqttWebSocketServerAdapter(this IServiceCollection services)
public static void AddMqttLogger(this IServiceCollection services, IMqttNetLogger logger)
{
services.AddSingleton<MqttWebSocketServerAdapter>();
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttWebSocketServerAdapter>());
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

return services;
services.AddSingleton(logger);
}

public static IServiceCollection AddMqttServer(this IServiceCollection serviceCollection, Action<MqttServerOptionsBuilder> configure = null)
{
if (serviceCollection is null)
{
throw new ArgumentNullException(nameof(serviceCollection));
}

serviceCollection.AddMqttConnectionHandler();
serviceCollection.AddHostedMqttServer(configure);

return serviceCollection;
}

public static IServiceCollection AddMqttTcpServerAdapter(this IServiceCollection services)
Expand All @@ -94,12 +118,12 @@ public static IServiceCollection AddMqttTcpServerAdapter(this IServiceCollection
return services;
}

public static IServiceCollection AddMqttConnectionHandler(this IServiceCollection services)
public static IServiceCollection AddMqttWebSocketServerAdapter(this IServiceCollection services)
{
services.AddSingleton<MqttConnectionHandler>();
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttConnectionHandler>());
services.AddSingleton<MqttWebSocketServerAdapter>();
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttWebSocketServerAdapter>());

return services;
}
}
}
}

0 comments on commit 306c5df

Please sign in to comment.