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 line
3.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. 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. var builder = new MqttServerOptionsBuilder();
  23. configure(builder);
  24. return builder.Build();
  25. });
  26. services.AddHostedMqttServer();
  27. return services;
  28. }
  29. public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure)
  30. {
  31. services.AddSingleton<IMqttServerOptions>(s => {
  32. var builder = new AspNetMqttServerOptionsBuilder(s);
  33. configure(builder);
  34. return builder.Build();
  35. });
  36. services.AddHostedMqttServer();
  37. return services;
  38. }
  39. public static IServiceCollection AddHostedMqttServer<TOptions>(this IServiceCollection services)
  40. where TOptions : class, IMqttServerOptions
  41. {
  42. services.AddSingleton<IMqttServerOptions, TOptions>();
  43. services.AddHostedMqttServer();
  44. return services;
  45. }
  46. private static IServiceCollection AddHostedMqttServer(this IServiceCollection services)
  47. {
  48. var logger = new MqttNetLogger();
  49. var childLogger = logger.CreateChildLogger();
  50. services.AddSingleton<IMqttNetLogger>(logger);
  51. services.AddSingleton(childLogger);
  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. }