protected override async Task RunAsync(CancellationToken cancellationToken) { this.cancellationToken = cancellationToken; var votes = await this.StateManager.GetOrAddAsync <IReliableQueue <Vote> >(votesName); while (!cancellationToken.IsCancellationRequested) { using (var transaction = StateManager.CreateTransaction()) { var vote = await votes.TryDequeueAsync(transaction, TimeSpan.FromMinutes(1), cancellationToken); if (vote.HasValue) { using (var db = new gerrymanderEntities()) { var candidate = db.Candidates.SingleOrDefault(c => c.Name == vote.Value.Candidate); if (candidate != null) { candidate.VoteCount = candidate.VoteCount + 1; } await db.SaveChangesAsync(); } } await transaction.CommitAsync(); } await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); } }
public ResultsController() { context = new gerrymanderEntities(); }