.NET MVC:在服务器上执行繁重的任务,然后在完成后向用户发送电子邮件,以便用户可以离开站点而不是等待?

.NET MVC: perform heavy task on server then email user when complete so that user can leave site instead of wait?

场景:一个按钮允许用户合并大量 PDF 文档以作为单个 PDF 下载。目前,获取所有 PDF 并合并的操作可能需要一分钟或更长时间,而用户必须等待下载开始。

我的目标是让用户想离开就离开。我想到的解决方案是在服务器后台合并文档,然后在完成后通过电子邮件 link 发送给用户,但我对其他解决方案持开放态度。

我不明白的是如何在后台异步执行合并。使用 .NET、MVC 5、DevExpress。

代码看起来有点像:

$.ajax({
   type: "POST",
   url: '@Url.Action("ExportMergedDocuments_PersonnelId", "Personnel", new { personnelId = Model.Id })',
}).done(function(data) {
   window.location.href = '@Url.RouteUrl(new { Controller = "Personnel", Action = "Download"})/?file=' + data.fileName; }
});
[HttpPost]
public JsonResult ExportMergedDocuments_PersonnelId(int PersonnelId)
{
   var allDocuments = new DataSet();
   allDocuments.Merge(GetDocuments((int)PersonnelId, ".....1").Tables[0]);
   allDocuments.Merge(GetDocuments((int)PersonnelId, ".....2").Tables[0]);

   string fileName = $"merged__{DateTime.Now.ToString("yyyyMMddHHmm")}.pdf";
   if (MergePdfSet(fileName, allDocuments))
      return Json(new { fileName });
   // else error msg
}

下载文件:

[HttpGet]
public ActionResult Download(string file)
{
   return File(..fullpath.., "application/pdf", file);
}

正在合并 PDF:

public bool MergePdfSet(string fileName, DataSet allDocuments)
{
   bool merged = false;
   string fullPath = Path.Combine(Server.MapPath("~/App_Data/temp/"), fileName);
   using (var pdfDocumentProcessor = new PdfDocumentProcessor())
   {
      pdfDocumentProcessor.CreateEmptyDocument(fullPath);
      foreach (DataRow row in allDocuments.Tables[0].Rows)
      {
         var documentId = (int)row["DocumentID"];
         var fetchedDocument = GetFile(documentId);
         pdfDocumentProcessor.AppendDocument(fetchedDocument);
         merged = true;
      }
   }
   return merged;
}

想到两个选项:

  1. 创建一个新线程并运行那里的代码但不要等待它。
  2. 使用Hangfire (https://www.hangfire.io/),你可以轻松入队。