You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

49 line
1.8 KiB

  1. using System;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Hosting;
  4. using MQTTnet.Adapter;
  5. using MQTTnet.Diagnostics;
  6. using MQTTnet.Server;
  7. using MQTTnet.Implementations;
  8. namespace MQTTnet.AspNetCore
  9. {
  10. public static class ServiceCollectionExtensions
  11. {
  12. public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, IMqttServerOptions options)
  13. {
  14. if (options == null) throw new ArgumentNullException(nameof(options));
  15. var logger = new MqttNetLogger();
  16. var childLogger = logger.CreateChildLogger();
  17. services.AddSingleton(options);
  18. services.AddSingleton<IMqttNetLogger>(logger);
  19. services.AddSingleton(childLogger);
  20. services.AddSingleton<MqttHostedServer>();
  21. services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
  22. services.AddSingleton<IMqttServer>(s => s.GetService<MqttHostedServer>());
  23. services.AddSingleton<MqttWebSocketServerAdapter>();
  24. services.AddSingleton<MqttTcpServerAdapter>();
  25. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttWebSocketServerAdapter>());
  26. if (options.DefaultEndpointOptions.IsEnabled)
  27. {
  28. services.AddSingleton<MqttTcpServerAdapter>();
  29. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttTcpServerAdapter>());
  30. }
  31. return services;
  32. }
  33. public static IServiceCollection AddMqttConnectionHandler(this IServiceCollection services)
  34. {
  35. services.AddSingleton<MqttConnectionHandler>();
  36. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttConnectionHandler>());
  37. return services;
  38. }
  39. }
  40. }