async Task Background(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { var allQuotesResult = await _priceRestClient.GetAll(); var openedOrders = await _dbAccess.GetAllOrdersByStatus(OrderStatus.Opened); foreach (var openedOrder in openedOrders) { var quote = allQuotesResult.Quotes.FirstOrDefault(f => f.Id == openedOrder.CoinId); var price = GetPrice(quote); openedOrder.Match(price); if (openedOrder.IsClosed) { await _publisherMessageBroker.PublishEvent(OrderMatched.FromOrder(openedOrder)); } await _dbAccess.Update(openedOrder); } await Task.Delay(1000, cancellationToken); } }