15

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: enter image description here enter image description here

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:

enter image description here

enter image description here

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

enter image description here

Cookies are totally empty (ASP.NET Session Id is absent) enter image description here

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"/>
Philipp Munin
  • 5,610
  • 7
  • 37
  • 60
  • If you use something like [Fiddler](http://www.telerik.com/fiddler) to call into the server method you will notice that the requests are queued on the client and not the server. Your server side code does allow for multiple requests, it's the browser that's queuing them up. Try the Composer in Fiddler to create your tests requests. – Janus Pienaar Nov 09 '15 at 07:40
  • More proof of this is if you set 'cache: false' on your ajax request. – Janus Pienaar Nov 09 '15 at 07:57
  • I don't get a delay at all. The problem is local to you. The code works fine for me. 3 seconds per request for however many requests. – rism Nov 09 '15 at 10:16
  • Perhaps you have your Max Worker Threads in the app pool set to 1? – Darren Gourley Nov 09 '15 at 20:07

2 Answers2

10

Concurrent parallel requests worked for me when I decorated my controller with this attribute

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]

It works better than the above disabling session state and was added back in MVC 3. More info here

RayLoveless
  • 19,880
  • 21
  • 76
  • 94
5

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.

Community
  • 1
  • 1
Jay
  • 2,644
  • 1
  • 30
  • 55
  • Hi. My use case is a bit different. I want that the subsequent requests are not queued, but sort of killed or not served at all. Can you help me do that? –  Sep 23 '16 at 09:37