|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using Microsoft.Extensions.DependencyInjection;
- using MQTTnet.Core.Adapter;
- using MQTTnet.Core.Client;
- using MQTTnet.Core.ManagedClient;
- using MQTTnet.Core.Serializer;
- using MQTTnet.Core.Server;
- using MQTTnet.Implementations;
- using System;
- using Microsoft.Extensions.Logging;
- using MQTTnet.Core.Diagnostics;
-
- namespace MQTTnet
- {
- public static class ServiceCollectionExtensions
- {
- public static IServiceCollection AddMqttServer(this IServiceCollection services)
- {
- services.AddMqttServerServices();
-
- services.AddSingleton<IMqttServer>(s => s.GetService<MqttServer>());
- services.AddSingleton<MqttServer>();
-
- return services;
- }
-
- public static IServiceCollection AddMqttServerServices(this IServiceCollection services)
- {
- services.AddOptions();
- services.AddSingleton<MqttFactory>();
- services.AddSingleton<IMqttCommunicationAdapterFactory>(s => s.GetService<MqttFactory>());
- services.AddSingleton<IMqttClientSesssionFactory>(s => s.GetService<MqttFactory>());
-
- services.AddTransient<IMqttServerAdapter, MqttServerAdapter>();
- services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
-
- services.AddTransient<MqttClientSessionsManager>();
- services.AddTransient<MqttClientSubscriptionsManager>();
- services.AddTransient<MqttClientRetainedMessagesManager>();
- return services;
- }
-
- public static IServiceCollection AddMqttServer(this IServiceCollection services, Action<MqttServerOptions> configureOptions)
- {
- return services
- .AddMqttServer()
- .Configure(configureOptions);
- }
-
- public static IServiceCollection AddMqttClient(this IServiceCollection services)
- {
- services.AddSingleton<MqttFactory>();
- services.AddSingleton<IMqttCommunicationAdapterFactory>(s => s.GetService<MqttFactory>());
-
- services.AddTransient<IMqttClient, MqttClient>();
- services.AddTransient<MqttClient>();
- services.AddTransient<IManagedMqttClient, ManagedMqttClient>();
- services.AddTransient<ManagedMqttClient>();
- services.AddTransient<IMqttPacketSerializer, MqttPacketSerializer>();
- services.AddTransient<MqttPacketDispatcher>();
-
- return services;
- }
-
- public static ILoggerFactory AddMqttTrace(this ILoggerFactory factory)
- {
- factory.AddProvider(new MqttNetTrace());
- return factory;
- }
- }
- }
|