gpt4 book ai didi

c# - 执行后端电子邮件进程而不让用户等待它完成

转载 作者:行者123 更新时间:2023-12-05 04:14:41 25 4
gpt4 key购买 nike

在我的 MVC 应用程序中,我具有由客户向各自的所有者/经销商发送消息的功能。 消息模型如下:

public class Messages
{
public string MessageFrom { get; set; }
public string MessageUserEmailID { get; set; }
public string MessageUserContactNum { get; set; }
public string Message { get; set; }
public int UserID { get; set; }//Admin user id
public string propertyName { get; set; }
}

下面是我的 Controller 方法,它将通过 AJAX 调用,其中

  • 我将消息存储在数据库中
  • 通过电子邮件向管理员发送通知
  • 返回一个 Json 响应给用户。

家庭 Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SendMessage([Bind(Prefix = "messageModel")]Messages model)
{
private SendNotification notify = new SendNotification();
if (ModelState.IsValid)
{
using (db)
{
tblMessage messageModel = new tblMessage();
//....
//....
//....
//fill the messageModel with the model values
db.tblMessages.Add(messageModel);
db.SaveChanges();
string toAddress = db.tblUsers.Where(u => u.UserId == model.UserID).Select(x => x.UserEmailID).FirstOrDefault();
if (!string.IsNullOrEmpty(toAddress))
{
notify.NotifyUser(toAddress, model.MessageUserContactNum, model.propertyName, model.Message, model.MessageFrom, model.MessageUserEmailID, "Kemmale Engineers' Notification - You have a message");
}
return Json(new { result = true, message = "Message sent! Thank you! We will get back to you soon!" }, JsonRequestBehavior.AllowGet);
}
}
return Json(new { result = false, message = "Server issue! Please try again later" }, JsonRequestBehavior.AllowGet);
}

SendNotification 类包含 NotifyUser 方法,如下所示:

public void NotifyUser(string toAddress, string userPhoneNum, string propertyName, string userMessage, string userName, string userContactEmail, string subject)
{
using (MailMessage mail = new MailMessage())
{
string Password = WebConfigurationManager.AppSettings["ApplicationEmailPassword"];
var securePassword = new SecureString();
Array.ForEach(Password.ToArray(), securePassword.AppendChar);
securePassword.MakeReadOnly();
mail.To.Add(toAddress);
mail.From = new MailAddress(WebConfigurationManager.AppSettings["ApplicationEmailID"]);
mail.Subject = subject;
mail.Body = Convert.ToString(ConstructBody(userMessage, propertyName, userPhoneNum, userContactEmail, userName));
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(Convert.ToString(mail.From), securePassword);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
}
}

如您所见,Notify 的返回类型为 void。现在我如何在不等待 Notify 方法执行的情况下将 Json 消息返回给用户,因为让用户等待内部进程完成是非常过时的,这是无关紧要的给用户?我在考虑 Asynchronous Task,但不确定如何在这里实现它。

最佳答案

如果你想执行函数而不是等待它完成,那么你必须在另一个线程中运行它。

最简单的方法是使用任务功能。

FW4.5以上可以使用

Task.Run(ActionToExecute)

或者如果你用的是FW4.0可以用

Task.Factory.StartNew(ActionToExecute).

否则,您可以使用线程池以老式方式完成:

ThreadPool.EnqueueUserWorkItem(ActionToExecute, null);

但在这种情况下, Action 必须有一个对象参数(即使它被忽略)。

关于c# - 执行后端电子邮件进程而不让用户等待它完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34639498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com