简体   繁体   English

在foreach .net 4.5 mvc中实现异步

[英]implement async in a foreach .net 4.5 mvc

in working to develop a new post method. 在努力开发一种新的职位方法。 I found that I am running into extremely long wait times on a bottleneck foreach statement. 我发现我在瓶颈foreach语句上遇到了非常长的等待时间。 I am saving between 5 and 8k(depending on the items being sent) to the database. 我将5到8k(取决于要发送的项目)保存到数据库中。 The method succeeds, but usually takes upwards of a full 60 seconds. 该方法成功,但是通常需要花费整整60秒的时间。 I have been looking into how to actually execute an async method, but I am not sure this will actually solve the problem. 我一直在研究如何实际执行异步方法,但是我不确定这是否能真正解决问题。

This is the full method 这是完整的方法

[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);
                }
            }

The Specific Foreach 具体的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");
            }

And the Database methods: 和数据库方法:

        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)
            {
            }
        }

My Question is two fold. 我的问题有两个方面。 First, is async a viable option with this type of Code (Using Sql server 2012) as I noticed in my research that if you have a database level bottleneck, async could potentially cause dead lock. 首先,正如我在研究中注意到的那样,使用这种类型的代码(使用Sql Server 2012)异步是一个可行的选择,如果您有数据库级瓶颈,异步可能会导致死锁。 Second, If Async/await is viable, what would be the best way to actually implement it? 其次,如果Async / await是可行的,那么实际实现它的最佳方法是什么?

Update : The post method is only retrieving the Id of the SystemGeneralAnnouncement, and the confirmation to start the method. 更新 :post方法仅检索SystemGeneralAnnouncement的ID,并确认启动该方法。 All of the actual execution is being handled inside the method itself. 所有实际执行都在方法本身内部进行处理。

Update 2: For clarifications Sake, these items are being passed to a task scheduler that fires at a later date, and takes all the stored items in the queue, then delivers them in the background. 更新2:为澄清起见,Sake将这些项目传递给任务调度程序,该任务调度程序将在以后触发,并将所有存储的项目放入队列中,然后在后台交付。 This is why I am using the SQL call. 这就是为什么我使用SQL调用。

if you have a database level bottleneck, async could potentially cause dead lock 如果您有数据库级别的瓶颈,异步可能会导致死锁

Not at all. 一点也不。

However, async won't help you run faster - that's a common misconception. 但是, async不会帮助您更快地运行-这是一个常见的误解。

What you want to do is a batch update. 您要做的是批量更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM