Asynchronous Controller is blocking requests in ASP.NET MVC through jQuery

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-05-28T08:59:51Z Indexed on 2010/05/28 9:01 UTC
Read the original article Hit count: 190

I have just started using the AsyncController in my project to take care of some long-running reports. Seemed ideal at the time since I could kick off the report and then perform a few other actions while waiting for it to come back and populate elements on the screen.

My controller looks a bit like this. I tried to use a thread to perform the long task which I'd hoped would free up the controller to take more requests:

public class ReportsController : AsyncController
{
    public void LongRunningActionAsync()
    {
        AsyncManager.OutstandingOperations.Increment();

        var newThread = new Thread(LongTask);
        newThread.Start();
    }

    private void LongTask()
    {
        // Do something that takes a really long time
        //.......

        AsyncManager.OutstandingOperations.Decrement();
    }

    public ActionResult LongRunningActionCompleted(string message)
    {
        // Set some data up on the view or something...

        return View();
    }

    public JsonResult AnotherControllerAction()
    {
        // Do a quick task...

        return Json("...");
    }
}

But what I am finding is that when I call LongRunningAction using the jQuery ajax request, any further requests I make after that back up behind it and are not processed until LongRunningAction completes. For example, call LongRunningAction which takes 10 seconds and then call AnotherControllerAction which is less than a second. AnotherControllerAction simply waits until LongRunningAction completes before returning a result.

I've also checked the jQuery code, but this still happens if I specifically set "async: true":

$.ajax({
    async: true,
    type: "POST",
    url: "/Reports.aspx/LongRunningAction",
    dataType: "html",
    success: function(data, textStatus, XMLHttpRequest) { 
           // ...
        },
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
       // ...
    }
});

At the moment I just have to assume that I'm using it incorrectly, but I'm hoping one of you guys can clear my mental block!

© Stack Overflow or respective owner

Related posts about c#

Related posts about JavaScript