示例#1
0
        private static ServiceProvider ConfigureServices(IServiceCollection serviceCollection)
        {
            // Build configuration
            configuration = new ConfigurationBuilder()
                            .SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
                            .AddJsonFile("appsettings.json", false)
                            .AddEnvironmentVariables()
                            .Build();

            // Add access to generic IConfigurationRoot

            var duration = configuration.GetValue <int>("NumberSenderSettings:Duration");
            var tbs      = configuration.GetValue <int>("NumberSenderSettings:TimeBetweenSending");

            var numberSenderSettings = new NumberSenderSettings(duration, tbs);

            var clientId  = configuration.GetValue <string>("EventPublisherSettings:ClientId");
            var host      = configuration.GetValue <string>("EventPublisherSettings:Host");
            var topicName = configuration.GetValue <string>("EventPublisherSettings:TopicName");

            var eventPublisherSettings = new EventPublisherSettings(clientId, host, topicName);

            serviceCollection.AddSingleton <IConfigurationRoot>(configuration);
            serviceCollection.AddLogging();
            serviceCollection.AddSingleton <NumberSenderSettings>(numberSenderSettings);
            serviceCollection.AddSingleton <EventPublisherSettings>(eventPublisherSettings);
            serviceCollection.AddSingleton <INumberGenerator, NumberGenerator>();
            serviceCollection.AddSingleton <IEventPublisher, EventPublisher>();
            serviceCollection.AddSingleton <INumberSenderService, NumberSenderService>();
            return(serviceCollection.BuildServiceProvider());
        }
示例#2
0
        public NumberSenderService(IEventPublisher eventPublisher, INumberGenerator numberGenerator, NumberSenderSettings settings)//
        {
            this._eventPublisher  = eventPublisher;
            this._numberGenerator = numberGenerator;
            this._settings        = settings;

            Console.WriteLine($"Initializing Numbersender, sending time: {this._settings.Duration}ms with {this._settings.TimeBetweenSending}ms intervall");
            Console.WriteLine("wait a few seconds until mosquitto broker is up an running..");
            System.Threading.Thread.Sleep(5000);
        }