コード例 #1
0
        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();
        }
コード例 #2
0
 private void DoWork()
 {
     try
     {
         WriteLog("Getting prices...");
         var prices  = FuelApiClient.GetPrices(DaysCount);
         var message = String.Join(Environment.NewLine, prices);
         WriteLog(message);
         var updated = PriceEditService.Add(prices);
         WriteLog($"Prices were updated - {updated}");
     }
     catch (Exception ex)
     {
         Logger?.Error(ex);
     }
     finally
     {
         Timer.Start();
     }
 }
コード例 #3
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...");
                }
            }
        }