25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

40 satır
1.4 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<IMqttServerAdapter>(s => s.GetService<MqttTcpServerAdapter>());
  29. }
  30. return services;
  31. }
  32. }
  33. }