public async Task OnResourceExecutionAsync(ResourceExecutingContext before, ResourceExecutionDelegate next) { var modifying = false; if (before.ActionDescriptor is ControllerActionDescriptor action) { modifying = action.MethodInfo.GetCustomAttributes <DatabaseUpdateAttribute>().Any(); } if (modifying) { using (var handle = await access.Write()) { var after = await next(); var status = after.HttpContext.Response.StatusCode; if (status < 400) { await SaveChanges(handle); } } } else { using (await access.Read()) { await next(); } } }
public override async Task Submit(IContextLock access) { if (pairing.Type != PairingType.Manual) { throw new InvalidEnumArgumentException("CreateManualPairs: pairing"); } using (var handle = await access.Write()) { entries.Entry(pairing) .LoadRelations(p => p.Pairs) .LoadRelations(p => p.World.Members, m => m.Pairs1) .LoadRelations(p => p.World.Members, m => m.Pairs2) .LoadRelations(p => p.World.Pairs) .LoadRelations(p => p.World.BackupUser); foreach (var pair in pairing.Pairs) { emails.Send(Emails.PairingReceiver, pair); emails.Send(Emails.PairingInitiator, pair); } pairing.IsComplete = true; await handle.Finish(); } }
public override async Task Submit(IContextLock access) { using (var handle = await access.Write()) { var repo = pairings.Pairs(pairing); foreach (var pair in pairs) { pair.Outcome = PairOutcome.Unknown; repo.Add(pair); emails.Send(Emails.PairingReceiver, pair); emails.Send(Emails.PairingInitiator, pair); } foreach (var member in optOuts) { member.OptOut = false; emails.Send(Emails.OptOut, member); } if (unpaired != null) { if (pairing.World.BackupUser != null) { emails.Send(Emails.PairingInitiator, unpaired, pairing.World.BackupUser); emails.Send(Emails.PairingReceiver, unpaired, pairing.World.BackupUser); } else { emails.Send(Emails.UnpairedMember, unpaired); } } pairing.IsComplete = true; await handle.Finish(); } }
public async Task Flush(int maxMillis) { var timer = new Stopwatch(); timer.Start(); while (timer.ElapsedMilliseconds < maxMillis) { var email = await Next(); if (email == null) { break; } var mime = new MailMessage { Subject = email.Subject, IsBodyHtml = true, Body = email.Body, From = new MailAddress(options.Smtp.Email, options.Smtp.Name), }; foreach (var recipient in email.Recipients) { mime.To.Add(new MailAddress(recipient.Address.Value, recipient.Name.Value)); } Send(mime); using (var handle = await access.Write()) { email.IsSent = true; email.Sent = DateTime.UtcNow; await handle.Finish(); } } }