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.
 
 
 
 

29 lines
1.0 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. namespace MQTTnet.AspNetCore
  8. {
  9. public static class ServiceCollectionExtensions
  10. {
  11. public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, IMqttServerOptions options)
  12. {
  13. if (options == null) throw new ArgumentNullException(nameof(options));
  14. services.AddSingleton(options);
  15. services.AddSingleton<IMqttNetLogger>(new MqttNetLogger());
  16. services.AddSingleton<MqttHostedServer>();
  17. services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
  18. services.AddSingleton<IMqttServer>(s => s.GetService<MqttHostedServer>());
  19. services.AddSingleton<MqttWebSocketServerAdapter>();
  20. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttWebSocketServerAdapter>());
  21. return services;
  22. }
  23. }
  24. }