在執行多個同時發生的Ajax調用時會導致我的MVC Web應用程序阻塞。我一直在讀,我發現兩個主題相同的問題在MVC3中禁用會話以允許多個AJAX調用
Asynchronous Controller is blocking requests in ASP.NET MVC through jQuery
他們被禁止使用ControllerSessionStateAttribute。我有嘗試使用該屬性的會議,但我的代碼是該解決方案仍然阻塞。您可以重現問題創建一個與下面的代碼
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
Example of error when calling multiple AJAX, try click quickly on both buttons until the server get blocked.
</p>
<button onclick="cuelga++;SetCallWaiting(cuelga);">Set a call waiting in the server</button><button onclick="libera++;ReleaseEveryone(libera);">Release all calls in the server</button>
<div id="text"></div>
<script type="text/javascript">
var cuelga = 0;
var libera =0;
function ReleaseEveryone(number) {
var url = "/Home/ReleaseEveryone/";
$.post(url, { "id": number },
ShowResult1, "json");
};
function SetCallWaiting(number) {
var url = "/Home/SetACallWaiting/";
$.post(url, { "id": number },
ShowResult, "json");
};
function ShowResult (data) {
$("#text").append(' [The call waiting number ' + data[0] + ' come back ] ');
/* When we come back we also add a new extra call waiting with number 1000 to make it diferent */
SetCallWaiting(1000);
};
function ShowResult1(data) {
$("#text").append(' [The release call number ' + data[0] + ' come back ] ');
};
</script>
新MVC3 Web應用程序,這是HomeController的
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Threading;
using System.Web.SessionState;
namespace ErrorExample.Controllers
{
[SessionState(SessionStateBehavior.Disabled)]
public class HomeController : Controller
{
private static List<EventWaitHandle> m_pool = new List<EventWaitHandle>();
private static object myLock = new object();
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
[HttpPost]
public JsonResult SetACallWaiting()
{
EventWaitHandle myeve;
lock (myLock)
{
myeve = new EventWaitHandle(false, EventResetMode.ManualReset);
m_pool.Add(myeve);
}
myeve.WaitOne();
var topic = HttpContext.Request.Form[0];
return Json(new object[] { topic });
}
[HttpPost]
public JsonResult ReleaseEveryone()
{
try
{
lock (myLock)
{
foreach (var eventWaitHandle in m_pool)
{
eventWaitHandle.Set();
}
m_pool.Clear();
}
var topic = HttpContext.Request.Form[0];
return Json(new object[] { topic });
}
catch (Exception)
{
return Json(new object[] { "Error" });
}
}
}
}
非常感謝你提前。