public async Task DoWork(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { executionCount++; _logger.LogInformation( "Scoped Processing Service is working. Count: {Count}", executionCount); BusinessProcess businessProcess = new BusinessProcess(); if (_context != null) { // var x = _context.Accounts.Count(); var paymentsToProcess = _context.BillPays.Where(x => x.BillPayStatus == BillPayStatus.Waiting); foreach (var paymentToProcess in paymentsToProcess) { switch (paymentToProcess.Period) { case "One Time": businessProcess.OneTimeProcess(paymentToProcess, _context); break; case "Monthly": // process this tx businessProcess.OneTimeProcess(paymentToProcess, _context); // schedule next tx if (paymentToProcess.BillPayStatus == BillPayStatus.Success) { paymentToProcess.ScheduleDate.AddMonths(1); paymentToProcess.BillPayStatus = BillPayStatus.Waiting; } break; case "Quarterly": // process this tx businessProcess.OneTimeProcess(paymentToProcess, _context); // schedule next tx if (paymentToProcess.BillPayStatus == BillPayStatus.Success) { paymentToProcess.ScheduleDate.AddMonths(3); paymentToProcess.BillPayStatus = BillPayStatus.Waiting; } break; case "Annually": //purposely schedule for 30s for demostration purposes // process this tx businessProcess.OneTimeProcess(paymentToProcess, _context); // schedule next tx if (paymentToProcess.BillPayStatus == BillPayStatus.Success) { paymentToProcess.ScheduleDate.AddMonths(12); paymentToProcess.BillPayStatus = BillPayStatus.Waiting; } break; default: break; } } await _context.SaveChangesAsync(); } await Task.Delay(10000, stoppingToken); } }