Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

71 rader
2.6 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.AddSingleton<MqttClientSessionsManager>();
  31. services.AddTransient<MqttClientSubscriptionsManager>();
  32. services.AddSingleton<IMqttClientRetainedMessageManager, MqttClientRetainedMessagesManager>();
  33. return services;
  34. }
  35. public static IServiceCollection AddMqttServer(this IServiceCollection services, Action<MqttServerOptions> configureOptions)
  36. {
  37. return services
  38. .AddMqttServer()
  39. .Configure(configureOptions);
  40. }
  41. public static IServiceCollection AddMqttClient(this IServiceCollection services)
  42. {
  43. services.AddSingleton<MqttFactory>();
  44. services.AddSingleton<IMqttCommunicationAdapterFactory>(s => s.GetService<MqttFactory>());
  45. services.AddTransient<IMqttClient, MqttClient>();
  46. services.AddTransient<MqttClient>();
  47. services.AddTransient<IManagedMqttClient, ManagedMqttClient>();
  48. services.AddTransient<ManagedMqttClient>();
  49. services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
  50. services.AddTransient<MqttPacketDispatcher>();
  51. return services;
  52. }
  53. public static ILoggerFactory AddMqttTrace(this ILoggerFactory factory)
  54. {
  55. factory.AddProvider(new MqttNetTrace());
  56. return factory;
  57. }
  58. }
  59. }