ASP.net运行缓慢方法的最佳方式

3

给定以下方法:

public static void SubmitNewTutorial()
{
    // Insert new tutorial method
    AddToDatabase();

    // Email notify anyone subscribed
    SendOutEmailNotifications();

    // Redirect to published tutorial page
    Redirect();
}

当订阅者数量超过数万时,SendOutEmailNotifications 可能会变得缓慢 (可能需要超过30秒)。

在调用 Redirect() 之前,如何最好地调用 SendOutEmailNotifications 而不必让作者等待30秒?

SendOutEmailNotifications 附加到一个新任务中是否是推荐的方法?


尝试在网络和本站上搜索“asp.net后台任务”。这是每天都被问到和回答的问题,不要成为一个统计数字。 - CodeCaster
1
SendOutEmailNotifications 应该在一些外部队列服务中排队,定期卸载邮件,而不是在同一个 ASP.NET 项目内部进行。 - Yuval Itzchakov
2个回答

1

您可以使用Hangfire

或者尝试:

public static void SubmitNewTutorial()
  {
    // Insert new tutorial method
    AddToDatabase();

    // Email notify anyone subscribed
    System.Web.Hosting.HostingEnvironment.QueueBackgroundWorkItem(ct => SendOutEmailNotifications());

    // Redirect to published tutorial page
    Redirect();
  }

0
如果速度较慢但不是太慢,我可能会像上面建议的那样在单独的线程上运行它。如果需要很长时间,将电子邮件详细信息写入数据库,然后有一个完全不同的进程从数据库获取这些详细信息并发送电子邮件。我经常使用Windows服务来完成这项工作。这样,无论它落后3秒还是3分钟,都没有关系。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接