异步 ActionResult 实现正在阻塞

Async ActionResult implementation is blocking

好的,

这里我有一个 MVC 4 应用程序,我正尝试在其中创建一个异步 ActionResult。

Objective : 用户在网页上有一个下载PDF的图标,下载很费时间。因此,当服务器忙于生成 PDF 时,用户应该能够在网页中执行一些操作。

(单击 "download PDF" link 正在向服务器发送 ajax 请求,服务器正在获取一些数据并正在推送 PDF)

发生的事情是,当我调用 ajax 下载 PDF 时,它会启动该过程,但会阻止每个请求,直到 returns 返回浏览器。那是简单的阻塞请求。

到目前为止我已经尝试了什么。

1) 使用 AsyncController 作为控制器的基础 class。

2) 将 ActionResult 生成为异步任务 DownloadPDF(),在这里我将整个 code/logic 包装到一个包装器中以生成 PDF。这个包装器最终是 DownloadPDF()

中一个值得等待的东西

像这样。

public async Task<ActionResult> DownloadPDF()
{
    string filepath = await CreatePDF();
    //create a file stream and return it as ActionResult
}

private async Task<string> CreatePDF()
{
    // creates the PDF and returns the path as a string
    return filePath;
}

是的,操作是基于会话的。

我是不是遗漏了什么地方?

Objective : User has a download PDF Icon on the WebPage, and downloading takes much of time. So while server is busy generating the PDF, the user shall be able to perform some actions in webpage.

async不会这样做。正如我在 my MSDN article, async yields to the ASP.NET runtime, not the client browser. This only makes sense; async can't change the HTTP protocol 中所描述的(正如我在我的博客中提到的)。

然而,虽然 async 不能做到这一点,但 AJAX 可以。

What is happening is while I call the ajax to download the PDF it starts the process, but blocks every request until and unless it returns back to the browser. That is simple blocking request.

据我所知,您发布的请求代码是完全异步的。在创建 PDF 时,它正在 return 将线程连接到 ASP.NET 线程池。但是,并发请求还有其他几个方面。特别是,一个常见的挂断是默认情况下 ASP.NET 会话状态不能在多个请求之间共享。

1) Used AsyncController as a base class of controller.

这是不必要的。现代控制器检查 return 类型的操作以确定它们是否异步。

YES, the Operations are session based.

在我看来,ASP.NET 会话限制了您的请求。参见 Concurrent Requests and Session State。您必须将其关闭或设为只读才能在同一会话中处理并发请求。