예제 #1
0
        private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action <AspCoreMqttClientOptionBuilder> configure)
        {
            services.AddSingleton(serviceProvider =>
            {
                var builder = new AspCoreMqttClientOptionBuilder(serviceProvider);
                configure(builder);
                return(builder.Build());
            });

            var factory = new MqttFactory();

            services.AddSingleton(factory.CreateMqttClient());
            services.AddHostedService <MqttWorker>();

            return(services);
        }
        private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action <AspCoreMqttClientOptionBuilder> configure)
        {
            services.AddSingleton <IMqttClientOptions>(serviceProvider =>
            {
                var optionBuilder = new AspCoreMqttClientOptionBuilder(serviceProvider);
                configure(optionBuilder);
                return(optionBuilder.Build());
            });
            services.AddSingleton <MqttClientService>();
            services.AddSingleton <IHostedService>(serviceProvider =>
            {
                return(serviceProvider.GetService <MqttClientService>());
            });

            return(services);
        }