public FuelBackgroundService(int daysCount, TimeSpan interval, FuelApiClient fuelApiClient, FuelPriceEditService priceEditService, ILog logger = null)
        {
            if (daysCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(daysCount));
            }
            else if (fuelApiClient == null)
            {
                throw new ArgumentNullException(nameof(fuelApiClient));
            }
            else if (priceEditService == null)
            {
                throw new ArgumentNullException(nameof(priceEditService));
            }

            DaysCount        = daysCount;
            Interval         = interval;
            FuelApiClient    = fuelApiClient;
            PriceEditService = priceEditService;
            Logger           = logger;
            Timer            = new Timer
            {
                AutoReset = false,
                Interval  = interval.TotalMilliseconds,
            };

            Timer.Elapsed += (sender, e) => DoWork();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var logger    = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            var apiClient = new FuelApiClient(Settings.Default.ApiUrl);

            using (var context = new FuelDbContext())
            {
                var editService = new FuelPriceEditService(context);
                using (var service = new FuelBackgroundService(Settings.Default.DaysCount, Settings.Default.TaskExecutionDelay, apiClient, editService, logger))
                {
                    service.Start();
                    PressAnyKeyForContinue("Press any key for stop.");
                    service.Stop();

                    var prices  = context.Prices.ToList();
                    var message = String.Join(Environment.NewLine, prices);
                    Console.WriteLine($"{Environment.NewLine}Fuel prices:");
                    Console.WriteLine(message);
                    PressAnyKeyForContinue("Press any key for exit...");
                }
            }
        }