using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using MQTTnet.Adapter; using MQTTnet.Diagnostics; using MQTTnet.Server; using MQTTnet.Implementations; namespace MQTTnet.AspNetCore { public static class ServiceCollectionExtensions { public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, IMqttServerOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); services.AddSingleton(options); services.AddHostedMqttServer(); return services; } public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action configure) { var builder = new MqttServerOptionsBuilder(); configure(builder); services.AddSingleton(builder.Build()); services.AddHostedMqttServer(); return services; } public static IServiceCollection AddHostedMqttServer(this IServiceCollection services) where TOptions : class, IMqttServerOptions { services.AddSingleton(); services.AddHostedMqttServer(); return services; } public static IServiceCollection AddHostedMqttServer(this IServiceCollection services) { var logger = new MqttNetLogger(); var childLogger = logger.CreateChildLogger(); services.AddSingleton(logger); services.AddSingleton(childLogger); services.AddSingleton(); services.AddSingleton(s => s.GetService()); services.AddSingleton(s => s.GetService()); return services; } public static IServiceCollection AddMqttWebSocketServerAdapter(this IServiceCollection services) { services.AddSingleton(); services.AddSingleton(s => s.GetService()); return services; } public static IServiceCollection AddMqttTcpServerAdapter(this IServiceCollection services) { services.AddSingleton(); services.AddSingleton(s => s.GetService()); return services; } public static IServiceCollection AddMqttConnectionHandler(this IServiceCollection services) { services.AddSingleton(); services.AddSingleton(s => s.GetService()); return services; } } }