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.
 
 
 
 

99 lines
3.3 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.Implementations;
  7. using MQTTnet.Server;
  8. namespace MQTTnet.AspNetCore.Extensions
  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.AddHostedMqttServer();
  17. return services;
  18. }
  19. public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action<MqttServerOptionsBuilder> configure)
  20. {
  21. services.AddSingleton<IMqttServerOptions>(s =>
  22. {
  23. var builder = new MqttServerOptionsBuilder();
  24. configure(builder);
  25. return builder.Build();
  26. });
  27. services.AddHostedMqttServer();
  28. return services;
  29. }
  30. public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure)
  31. {
  32. services.AddSingleton<IMqttServerOptions>(s =>
  33. {
  34. var builder = new AspNetMqttServerOptionsBuilder(s);
  35. configure(builder);
  36. return builder.Build();
  37. });
  38. services.AddHostedMqttServer();
  39. return services;
  40. }
  41. public static IServiceCollection AddHostedMqttServer<TOptions>(this IServiceCollection services)
  42. where TOptions : class, IMqttServerOptions
  43. {
  44. services.AddSingleton<IMqttServerOptions, TOptions>();
  45. services.AddHostedMqttServer();
  46. return services;
  47. }
  48. private static IServiceCollection AddHostedMqttServer(this IServiceCollection services)
  49. {
  50. var logger = new MqttNetLogger();
  51. services.AddSingleton<IMqttNetLogger>(logger);
  52. services.AddSingleton<MqttHostedServer>();
  53. services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
  54. services.AddSingleton<IMqttServer>(s => s.GetService<MqttHostedServer>());
  55. return services;
  56. }
  57. public static IServiceCollection AddMqttWebSocketServerAdapter(this IServiceCollection services)
  58. {
  59. services.AddSingleton<MqttWebSocketServerAdapter>();
  60. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttWebSocketServerAdapter>());
  61. return services;
  62. }
  63. public static IServiceCollection AddMqttTcpServerAdapter(this IServiceCollection services)
  64. {
  65. services.AddSingleton<MqttTcpServerAdapter>();
  66. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttTcpServerAdapter>());
  67. return services;
  68. }
  69. public static IServiceCollection AddMqttConnectionHandler(this IServiceCollection services)
  70. {
  71. services.AddSingleton<MqttConnectionHandler>();
  72. services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttConnectionHandler>());
  73. return services;
  74. }
  75. }
  76. }