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.

ServiceCollectionExtensions.cs 1.2 KiB

преди 7 години
преди 6 години
преди 7 години
преди 7 години
преди 6 години
12345678910111213141516171819202122232425262728293031
  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. services.AddSingleton(options);
  16. services.AddSingleton<IMqttNetLogger>(new MqttNetLogger());
  17. services.AddSingleton<MqttHostedServer>();
  18. services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
  19. services.AddSingleton<IMqttServer>(s => s.GetService<MqttHostedServer>());
  20. services.AddSingleton<MqttWebSocketServerAdapter>();
  21. services.AddSingleton<MqttTcpServerAdapter>();
  22. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttWebSocketServerAdapter>());
  23. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttTcpServerAdapter>());
  24. return services;
  25. }
  26. }
  27. }