Example #1
0
 public static IHostBuilder CreateHostBuilder(string[] args) =>
 Host.CreateDefaultBuilder(args)
 .UseSystemd()                         // Linux service lifetime management
 .ConfigureAppConfiguration((context, builder) =>
 {
     builder
     .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))                                     // Required for Linux service
     .AddJsonFile("appsettings.json")
     .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json")
     .AddEnvironmentVariables()
     .Build();
 })
 .ConfigureServices((hostContext, services) =>
 {
     var philipsHueConfig = new PhilipsHueConfiguration();
     hostContext.Configuration.GetSection("PhilipsHue").Bind(philipsHueConfig);
     services.AddSingleton(philipsHueConfig);
     var serviceConfig = new ServiceConfiguration();
     hostContext.Configuration.GetSection("Service").Bind(serviceConfig);
     services.AddSingleton(serviceConfig);
     var weatherProviderConfig = new WeatherProviderConfiguration();
     hostContext.Configuration.GetSection("WeatherProvider").Bind(weatherProviderConfig);
     services.AddSingleton(weatherProviderConfig);
     services.AddHttpClient();
     services.AddSingleton(Database.GetDbContextOptions <WeatherDbContext>(hostContext.Configuration.GetConnectionString("Db")));
     services.AddSingleton <DataCollectionService>();
     services.AddSingleton <DataAggregationService>();
     services.AddSingleton <DataSourcesService>();
     services.AddHostedService <DataCollectionWorker>();
     services.AddHostedService <DataAggregationWorker>();
 });
		public DataCollectionService(ILogger<DataCollectionService> logger, PhilipsHueConfiguration config, DbContextOptions<WeatherDbContext> dbOptions, IHttpClientFactory clientFactory, WeatherProviderConfiguration weatherConfiguration)
		{
			if (logger == null) throw new ArgumentNullException(nameof(logger));
			if (config == null) throw new ArgumentNullException(nameof(config));
			if (dbOptions == null) throw new ArgumentNullException(nameof(dbOptions));
            if (clientFactory == null) throw new ArgumentNullException(nameof(clientFactory));
            if (weatherConfiguration == null) throw new ArgumentNullException(nameof(weatherConfiguration));
            _logger = logger;
			_config = config;
			_dbOptions = dbOptions;
			_hueClient = new LocalHueClient(_config.BridgeIp);
			_hueClient.Initialize(_config.AppKey);
            _clientFactory = clientFactory;
            _weatherConfig = weatherConfiguration;
        }