No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

ServiceCollectionExtensions.cs 1.8 KiB

hace 6 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. }