异步控制器通过封锁的jQuery在ASP.NET MVC的请求 [英] Asynchronous Controller is blocking requests in ASP.NET MVC through jQuery

查看:156
本文介绍了异步控制器通过封锁的jQuery在ASP.NET MVC的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用AsyncController在我的项目采取的一些长期运行的报告服务。似乎在理想的时间,因为我可以揭开序幕报告,然后执行一些其他操作,而等待它回来,并填充屏幕上的元素。

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("...");
    }
}

但我发现的是,当我打电话使用jQuery Ajax请求LongRunningAction,任何进一步的要求我以后,使备份其背后,不会被处理LongRunningAction直到完成。例如,呼叫LongRunningAction这需要10秒钟,然后调用AnotherControllerAction小于一秒钟。 AnotherControllerAction只是等待,直到LongRunningAction返回结果之前完成。

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.

我也检查了jQuery的code,但如果我专门设置这个仍然发生异步:真正的

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!

推荐答案

有在玩两个问题在这里。第一个是你的控制器是不是真正的异步。纺纱一个线程池线程来执行工作一般具有的更糟的不止做从操作方法本身的一切,你仍然从ASP.NET(这只是共享CLR线程池采取线程池资源的性能特点)的的你现在迫使CLR和兼顾线程操作系统。见<一href=\"http://msdn.microsoft.com/en-us/library/ee728598.aspx#choosing_synchronous_or_asynchronous_action_methods\">http://msdn.microsoft.com/en-us/library/ee728598.aspx#choosing_synchronous_or_asynchronous_action_methods了解更多信息。基本上,该链接可以归结为是,如果你不能使用I / O完成端口为您的异步操作,你不太可能看到更高的性能。

There are two issues in play here. The first is that your controller is not truly asynchronous. Spinning up a ThreadPool thread to perform work generally has worse performance characteristics than just doing everything from within the action method itself, as you're still taking ThreadPool resources from ASP.NET (which just shares the CLR ThreadPool), and you're now forcing the CLR and the OS to juggle threads. See http://msdn.microsoft.com/en-us/library/ee728598.aspx#choosing_synchronous_or_asynchronous_action_methods for more information. Basically, what that link boils down to is that if you can't use I/O completion ports for your asynchronous operations, you're very unlikely to see improved performance.

第二个问题是,ASP.NET MVC对所有的请求的会话锁。一个会话中的多个请求都将被序列化,否则,如果一个控制器作为另一个控制器试图读取写入到会话的用户的会话可能会损坏。请参见 http://forums.asp.net/t/1501623.aspx 上下文和解决方法。 MVC 2期货交易具有禁用此锁的方式;它也可以被包括在MVC的3正确。见<一href=\"https://blogs.msdn.com/b/rickandy/archive/2009/12/17/session-less-mvc-controller.aspx\">https://blogs.msdn.com/b/rickandy/archive/2009/12/17/session-less-mvc-controller.aspx更多这方面的信息。

The second issue is that ASP.NET MVC takes a Session lock on all requests. Multiple requests within a single Session will always be serialized, as otherwise the user's Session could become corrupted if one controller writes to Session as another controller is trying to read it. See http://forums.asp.net/t/1501623.aspx for context and a workaround. MVC 2 Futures has a way of disabling this lock; it may also be included in MVC 3 proper. See https://blogs.msdn.com/b/rickandy/archive/2009/12/17/session-less-mvc-controller.aspx for more information on this.

这篇关于异步控制器通过封锁的jQuery在ASP.NET MVC的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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