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.
 
 
 
 

69 regels
2.4 KiB

  1. using Microsoft.Extensions.DependencyInjection;
  2. using MQTTnet.Core.Adapter;
  3. using MQTTnet.Core.Client;
  4. using MQTTnet.Core.ManagedClient;
  5. using MQTTnet.Core.Serializer;
  6. using MQTTnet.Core.Server;
  7. using MQTTnet.Implementations;
  8. using System;
  9. using Microsoft.Extensions.Logging;
  10. using MQTTnet.Core.Diagnostics;
  11. namespace MQTTnet
  12. {
  13. public static class ServiceCollectionExtensions
  14. {
  15. public static IServiceCollection AddMqttServer(this IServiceCollection services)
  16. {
  17. services.AddMqttServerServices();
  18. services.AddSingleton<IMqttServer>(s => s.GetService<MqttServer>());
  19. services.AddSingleton<MqttServer>();
  20. return services;
  21. }
  22. public static IServiceCollection AddMqttServerServices(this IServiceCollection services)
  23. {
  24. services.AddOptions();
  25. services.AddSingleton<MqttFactory>();
  26. services.AddSingleton<IMqttCommunicationAdapterFactory>(s => s.GetService<MqttFactory>());
  27. services.AddSingleton<IMqttClientSesssionFactory>(s => s.GetService<MqttFactory>());
  28. services.AddTransient<IMqttServerAdapter, MqttServerAdapter>();
  29. services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
  30. services.AddTransient<MqttClientSessionsManager>();
  31. services.AddTransient<MqttClientRetainedMessagesManager>();
  32. return services;
  33. }
  34. public static IServiceCollection AddMqttServer(this IServiceCollection services, Action<MqttServerOptions> configureOptions)
  35. {
  36. return services
  37. .AddMqttServer()
  38. .Configure(configureOptions);
  39. }
  40. public static IServiceCollection AddMqttClient(this IServiceCollection services)
  41. {
  42. services.AddSingleton<MqttFactory>();
  43. services.AddSingleton<IMqttCommunicationAdapterFactory>(s => s.GetService<MqttFactory>());
  44. services.AddTransient<IMqttClient, MqttClient>();
  45. services.AddTransient<MqttClient>();
  46. services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
  47. services.AddTransient<ManagedMqttClient>();
  48. services.AddTransient<MqttPacketDispatcher>();
  49. return services;
  50. }
  51. public static ILoggerFactory AddMqttTrace(this ILoggerFactory factory)
  52. {
  53. factory.AddProvider(new MqttNetTrace());
  54. return factory;
  55. }
  56. }
  57. }