在 foreach .net 4.5 mvc 中实现异步

implement async in a foreach .net 4.5 mvc

正在开发一种新的 post 方法。我发现我 运行 在瓶颈 foreach 语句上等待时间非常长。我正在向数据库保存 5 到 8k(取决于发送的项目)。该方法成功,但通常需要整整 60 秒以上的时间。我一直在研究如何实际执行异步方法,但我不确定这是否真的能解决问题。

这是完整的方法

[HttpPost]
        public ActionResult ConfirmSend(int? SystemGeneralAnnouncementId) { 
        var systemGeneralAnnouncement = (SystemGeneralAnnouncementId == null) ? null : _uow.SystemGeneralAnnouncementRepository.GetById(SystemGeneralAnnouncementId.Value);
        List<Status> status = new List<Status>();

            if (systemGeneralAnnouncement.Statuses.Length > 0)
            {
                status.AddRange(systemGeneralAnnouncement.Statuses.Split(',').Select(item => (Status) Enum.Parse(typeof (Status), item)));
            }

            var allEmailAddresses = new List<PointOfContact>();
            var EmailAddresses = new List<PointOfContact>();


            var result = new List<PointOfContact>();
            foreach (var item in status)
            {
                result = _uow.PointOfContactRepository.GetAllByStatus(item).ToList();
                allEmailAddresses.AddRange(result);
            }


            if (systemGeneralAnnouncement.SendToRecipients.Contains("(1) All Three Contact Types"))
            {
                mailAddresses = allEmailAddresses;
            }
            else
            {
                if (systemGeneralAnnouncement.SendToRecipients.Contains("(2) All Contacts "))
                {
                    EmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Primary).ToList());
                }
                if (systemGeneralAnnouncement.SendToRecipients.Contains("(3) All Compliance contacts"))
                {
                    pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Secondary).ToList());
                }
                if (systemGeneralAnnouncement.SendToRecipients.Contains("(4) All Authorities"))
                {
                    pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.SigningAuthority).ToList());
                }
                if (systemGeneralAnnouncement.SendToRecipients.Contains("(5) All Rate Contacts"))
                {
                    EmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.TuitionRates).ToList());
                }
                if (systemGeneralAnnouncement.SendToRecipients.Contains("(6) Specified Email Address"))
                {
                    var pocs = new List<PointOfContact>();

                    string[] emails = systemGeneralAnnouncement.EmailAddresses.Split(',');

                    foreach (string email in emails)
                    {                      
                        var addContact = new InstitutionPointOfContact { Email = email };

                        User user = _uow.UserRepository.GetByEmail(email);

                        if (user == null)
                        {
                            addContact.FirstName = "Not Created Account Yet";
                        }
                        else
                        {
                            addContact.FirstName = user.FirstName;
                            addContact.LastName = user.LastName;
                        }

                        List<PointOfContact> idAssociatedToUser =
                            _uow.PointOfContactRepository
                                .GetAllByEmail(email)
                                .ToList();


                        if (idAssociatedToUser.Count == 0)
                        {
                            addContact.IDNumber = "N/A";
                        }
                        else
                        {
                            string[] opeidArray = opeidAssociatedToUser
                                .Select(x => x.OPEIDNumber)
                                .ToArray();

                            addContact.OPEIDNumber = string.Join(",", opeidArray);
                        }
                        Contacts.Add(addContact);
                    }                  
                    EmailAddresses.AddRange(Contacts);
                }
            }

具体Foreach

    if (EmailAddresses.Count > 0)
                {
                    foreach (PointOfContact emailAddress in EmailAddresses.Where(x => x.Email != "").ToList())
                    {
                        string firstName = emailAddress.FirstName == null ? "" : emailAddress.FirstName.Trim();
                        string lastName = emailAddress.LastName == null ? "" : emailAddress.LastName.Trim();

                        string userName = firstName + " " + lastName;

                        string emailBody = WebUtility.HtmlDecode(systemGeneralAnnouncement.EmailBody);

                        SaveToDatabase(emailAddress.Email, emailBody, systemGeneralAnnouncement.Subject, UserIdentityHelper.GetUserEmailAddress + " (" + UserIdentityHelper.GetUserId + ")", systemGeneralAnnouncement.SystemGeneralAnnouncementId, userName, emailAddress.OPEIDNumber);
                        LogInstitutionEmail(systemGeneralAnnouncement.Subject, emailBody, emailAddress.Email, emailAddress.OPEIDNumber, systemGeneralAnnouncement.EmailAttachmentLocation);
                    }
                }
                return View("GeneralAnnouncementGeneratedConfirmation");
            }

和数据库方法:

        private void LogInstitutionEmail(string subject, string emailBody, string email, string opeidNumber, string emailAttachment)
        {
            try
            {
                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MasterContext"].ConnectionString))
                {
                    conn.Open();
                    var cmd = new SqlCommand("Insert Into InstitutionEmails (Since, Subject, Email, EmailAddress, OpeidNumber, FirstReadDateTime, Attachment) VALUES(@Since, @Subject, @Email, @EmailAddress, @idNumber, NULL, @Attachment)", conn);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Since", Value = DateTime.Now });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Subject", Value = subject });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Email", Value = emailBody });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmailAddress", Value = email });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@idNumber", Value = idNumber });

                    if (!string.IsNullOrEmpty(emailAttachment))
                    {
                        cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Attachment", Value = emailAttachment });
                    }

                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void SaveToDatabase(string emailRecipient, string emailBody, string subject, string userWhoSentIt, int systemGeneralAnnouncementId, string userName, string opeidNumber)
        {
            try
            {
                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MasterContext"].ConnectionString))
                {
                    conn.Open();
                    var cmd = new SqlCommand("Insert Into EmailQueue (EmailRecipients, EmailBody, EmailSubject, UserWhoSentIt, QueueDate, SystemGeneralAnnouncementId, UserName, idNumber) VALUES(@EmailRecipients, @EmailBody, @EmailSubject, @UserWhoSentIt, @QueueDate, @SystemGeneralAnnouncementId, @UserName, @idNumber)", conn);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add(new SqlParameter() {ParameterName = "@EmailRecipients", Value = emailRecipient });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmailBody", Value = emailBody });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmailSubject", Value = subject });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@UserWhoSentIt", Value = userWhoSentIt });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@QueueDate", Value = DateTime.Now });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@SystemGeneralAnnouncementId", Value = systemGeneralAnnouncementId });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@UserName", Value = userName });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@idNumber", Value = idNumber });


                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
            }
        }

我的问题有两个。首先,异步是否是此类代码(使用 Sql 服务器 2012)的可行选项,因为我在研究中注意到,如果您有数据库级别的瓶颈,异步可能会导致死锁。第二,如果 Async/await 可行,实际实施它的最佳方法是什么?

Update: post 方法仅检索 SystemGeneralAnnouncement 的 Id,以及启动该方法的确认。所有实际执行都在方法本身内部处理。

更新 2: 为了澄清起见,这些项目被传递给任务调度程序,该任务调度程序将在以后触发,并获取队列中的所有存储项目,然后交付他们在后台。这就是我使用 SQL 调用的原因。

if you have a database level bottleneck, async could potentially cause dead lock

完全没有。

但是,async 不会帮助您 运行 更快 - 这是一个常见的误解。

你要做的是批量更新。