【发布时间】:2013-06-11 17:09:55
【问题描述】:
我有一个针对 ASP.NET WebMethod 的 JQuery AJAX 调用。
我理解async: true 允许网页在函数返回之前不会被锁定,因此我将其设置为'true'。
我面临的问题是GetVersion()内部的内部方法GetVersionFromDataBase()需要很长时间才能完成,同时整个后端被锁定(单线程)。
用户体验是,对于正在浏览网站的其他用户,即使在不同的页面上,他们的屏幕也会被锁定,直到 'heavy' 方法返回。
如何使函数GetVersionFromDataBase() 位于不同的线程/任务中?
$.ajax({
type: "POST",
url: "Default.aspx/GetVersion",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (result) {
alert('the version is:' + result.d);
}
});
public partial class Default : System.Web.UI.Page
{
[WebMethod]
public string GetVersion()
{
string version = GetVersionFromDatabase(); // this is a heavy method freezes the program till it finishes
return version;
}
}
【问题讨论】:
-
当您说“整个后端被锁定(单线程)”时,您的意思是这发生在IIS的生产环境中吗?或者这只是发生在您的开发 Web 服务器中?每个请求都应该由 IIS 异步处理。
-
Rob,它发生在我的开发 Web 服务器上。我在调用该方法并且整个网站挂起时打开了一个新选项卡。您认为它在生产 IIS 上的作用会有所不同吗?
-
根据我的经验,它只发生在开发服务器中。看到这个帖子:stackoverflow.com/questions/9036150/…
-
我在下面添加了一个答案,如果你觉得是正确的答案,请标记它。