Example #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());
        }
Example #2
0
 public EventPublisher(EventPublisherSettings settings)
 {
     this._settings = settings;
     Console.WriteLine($"Initializing EventPublisher with ID {settings.ClientId}, URL {settings.Host} and Topic {settings.TopicName}");
 }