即使禁用会话,ASP.NET MVC 5 并发请求也会排队 [英] ASP.NET MVC 5 concurrent requests are queued even with disabled Session
问题描述
在考虑投反对票或告诉我谷歌它"之前,请更仔细地阅读问题.这是旧/经典问题,但旧/经典解决方案不再有效.这是在 Visual Studio 2013/2015 中重现的非常简单的场景:
Before thinking about downvoting or telling me "google it", please read the problem more carefully. This is old/classic problem but old/classic solution is no longer working. Here is very simple scenario to reproduce in Visual Studio 2013/2015:
1) 使用 MVC 模板创建 ASP.NET Web 应用程序:
1) Create ASP.NET Web application using MVC template:
2) 打开 ControllersHomeController.cs 并添加属性到控制器和睡眠"动作:
2) Open ControllersHomeController.cs and add attribute to controller and "Sleep" action:
[SessionState( System.Web.SessionState.SessionStateBehavior.Disabled)]
public class HomeController : Controller
{
public ActionResult Sleep(int? time)
{
System.Threading.Thread.Sleep(time ?? 3000);
return Content("OK");
}
public ActionResult Index()
{
...
3) 打开文件:ViewsHomeIndex.cshtml 并使用以下内容添加/替换内容 html:
3) Open file: ViewsHomeIndex.cshtml and add/replace content html with the following :
<script>
function ReqClick() {
var startTime = Date();
$.ajax("/Home/Sleep")
.success(function () {
var log = $("#log");
var endTime = Date();
log.text(log.text() + "Start: " + startTime.toString() + " === " + endTime.toString());
});
};
</script>
<button type="button" onclick="ReqClick();">
Request
</button>
<div>
<textarea id="log" style="width:640px; height:480px"></textarea>
</div>
4) 运行它(使用 IIS 或 IIS Express 或 Vs Dev Server 无关紧要)- 打开主页/索引.点击 F12 打开开发工具,打开网络选项卡.在主页上快速单击请求"按钮两次.你可以看到第二个请求花费了将近 6 秒:
在控制器的调试模式下,您可以看到会话为空:
Cookie 完全为空(ASP.NET 会话 ID 不存在)
Please let me know what I'm missing?
请告诉我我遗漏了什么?
Adding the setting below to web.config does not help either:
将以下设置添加到 web.config 也无济于事:
<sessionState mode="Off"/>
<pages enableSessionState="ReadOnly"/>
推荐答案
当我用这个属性装饰我的控制器时,并发并行请求对我有用
Concurrent parallel requests worked for me when I decorated my controller with this attribute
[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
它比上述禁用会话状态效果更好,并在 MVC 3 中重新添加.更多信息在这里
It works better than the above disabling session state and was added back in MVC 3. More info here
这篇关于即使禁用会话,ASP.NET MVC 5 并发请求也会排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!