ASP.NET MVC 5并发请求排队,即使禁用会话 [英] ASP.NET MVC 5 concurrent requests are queued even with disabled Session

查看:818
本文介绍了ASP.NET MVC 5并发请求排队,即使禁用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在考虑downvoting或告诉我google it之前,请仔细阅读问题。这是老/经典问题,但旧/经典解决方案不再工作。下面是在Visual Studio 2013/2015中重现的非常简单的场景:



1)使用MVC模板创建ASP.NET Web应用程序:



2)打开Controllers \HomeController.cs并添加属性控制器和休眠操作:

  [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)打开文件:Views\Home\Index.cshtml并用以下内容添加/替换内容html:



 < script> function ReqClick(){var startTime = Date(); $ .ajax(/ Home / Sleep).success(function(){var log = $(#log); var endTime = Date(); log.text(log.text + startTime.toString()+===+ endTime.toString());}); };< / script>< button type =buttononclick =ReqClick();>请求< / button>< div> < textarea id =logstyle =width:640px; height:480px>< / textarea>< / div>   



4)运行它(如果你使用IIS或IIS Express或Vs Dev Server无关紧要) - 打开首页/索引。单击F12打开开发工具,打开网络选项卡。在主页上单击请求按钮快两次。您可以看到第二个请求需要大约6秒:







在控制器的调试模式下,您可以看到Session为null:





Cookie完全为空(ASP.NET会话ID不存在)



请让我知道我错过了什么?



将以下设置添加到web.config并不会有帮助:


  sessionState mode =Off/> 
< pages enableSessionState =ReadOnly/>



解决方案

很多时间在这个问题,发现你在上面看到的很多信息也没有纠正这个问题。我的并行请求总是排队,我在Internet Explorer和Firefox中看到了这一点。



我发现这篇文章与使用MVC应用程式相关本文



这表明您可以使用Disabled [SessionState (SessionStateBehavior.Disabled)] ,而不是设置为ReadOnly。



我将执行AJAX GET调用的代码移动到自己的控制器,因为我在当前控制器中使用了其他代码,使用 Session 通过 TempData 。这纠正了我的问题,并且我的 $。get()调用到会话被禁用的控制器将与 $。post() code>给使用会话的其他控制器。


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) Create ASP.NET Web application using MVC template:

2) Open Controllers\HomeController.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) Open file: Views\Home\Index.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) Run it (does not matter if you're using IIS or IIS Express or Vs Dev Server) - Open Home/Index. Click F12 to open dev tool, open network tab. On the Home page click "Request" button twice fast. You can see that second request takes almost 6 seconds:

In Debug mode in controller you can see that Session is null:

Cookies are totally empty (ASP.NET Session Id is absent)

Please let me know what I'm missing?

Adding the setting below to web.config does not help either:

<sessionState mode="Off"/>
<pages enableSessionState="ReadOnly"/>

解决方案

I spent a lot of time on this issue and found much of the information that you saw above also did not correct the issue. My parallel requests were always queuing, I saw this in Internet Explorer as well as Firefox.

I found this article related to am using an MVC application this article

This shows that you can use Disabled [SessionState(SessionStateBehavior.Disabled)] instead of setting it to ReadOnly.

I moved my code that was performing the AJAX GET call to its own controller since I had other code in the current controller that used Session via TempData. This corrected my issue and my $.get() call to the controller with session disabled would process in parallel with the $.post() to the other controller that used session.

这篇关于ASP.NET MVC 5并发请求排队,即使禁用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆