ASP.NET MVC + jQuery + IIS6:多个Ajax请求

时间:2022-09-03 04:14:46

I'm not sure where the problem is...

我不确定问题出在哪里......

I have an ajax request that checks the tracking information for a package on a page with a list of packages:

我有一个ajax请求,它检查包含一个包列表的页面上包的跟踪信息:

$(".fedex_status").each(function() {

    var item = this;

    // some code to construct tracking_url

    $.ajax({
         type: "GET", 
         url: tracking_url, 
         async: true, 
         cache: false, 
         success: function(data) { $(item).html("(" + data  + ")"); }, 
         error: function() { $(item).html("request failed...");} 
         });
 });

So if there are 10 packages on the page (10 things with class 'fedex_status') 10 requests are created. The requests work fine, but results are returned one at a time (in a serial manner). I added a timestamp to the start and stop of a request within the controller action:

因此,如果页面上有10个包(10个具有类'fedex_status'的东西),则会创建10个请求。请求工作正常,但结果一次返回一个(以串行方式)。我在控制器操作中为请求的开始和停止添加了一个时间戳:

    public ActionResult Fedex(string trackingNumber)
    {
        DateTime requestStart = DateTime.Now;

        TrackingService tracking = new TrackingService();

        string status = tracking.FedexTrackingNumberStatus(trackingNumber);

        return Content(status + " - " + requestStart.ToString("hh:mm:ss.FFF") + " - " + DateTime.Now.ToString("hh:mm:ss.FFF"));
    }

None of the timestamps overlap. So the controller is processing the requests one at a time. This seems crappy.

没有时间戳重叠。因此,控制器一次处理一个请求。这看起来很糟糕。

Now, the ajax request 'should' be parallel. It definitely returns immediately (its asynchronous). When I look at the IIS logs the requests have the same timestamp as the controller action return.

现在,ajax请求'应该'是并行的。它肯定会立即返回(它的异步)。当我查看IIS日志时,请求具有与控制器操作返回相同的时间戳。

So my question is: is jquery not sending all the ajax requests in parallel or is IIS or ASP.NET only processing requests serially. I'm a big noob with IIS and the technical details of ASP.NET, so maybe its been misconfigured forever and only responds to one request at a time on everything (its internal use only, low traffic). That said, I'm also an idiot at jquery and not sure how to test when the requests are actually being fired (all I know is the $.ajax call returns immediately).

所以我的问题是:jquery不是并行发送所有的ajax请求,或者是IIS或ASP.NET只是串行处理请求。我是IIS的大菜鸟和ASP.NET的技术细节,所以它可能永远错误配置,一次只响应一个请求(仅限内部使用,流量低)。也就是说,我也是jquery的白痴,不确定如何测试请求实际被解雇(我所知道的是$ .ajax调用立即返回)。

Thanks!

3 个解决方案

#1


In theory, ASP.NET with Session state enabled locks the session for each user request, thus processing them serially. I don't think you can disable Session for an MVC project painlessly, because TempData for example uses session by default. But you can try.

理论上,启用了会话状态的ASP.NET会锁定每个用户请求的会话,从而以串行方式处理它们。我不认为你可以无痛地禁用MVC项目的Session,因为TempData例如默认使用session。但你可以试试。

EDIT: here's a related question

编辑:这是一个相关的问题

#2


Your code works as expected on my machine. I had to insert any artificial delay with Thread.Sleep to see it in action though because the unadulterated requests executed so fast that it appeared they were all completed at once.

您的代码在我的机器上按预期工作。我不得不使用Thread.Sleep插入任何人工延迟来查看它的运行情况,因为未执行的请求执行得如此之快,以至于它们似乎都立即完成了。

 System.Threading.Thread.Sleep(1000);

 return Content(String.Format("Thread #{0}, Started = {1:hh:mm:ss.FFF}, Completed = {2:hh:mm:ss.FFF}, Duration = {3:hh:mm:ss.FFF}, Result = {4}",
       System.Threading.Thread.CurrentThread.ManagedThreadId,
       requestStart,
       DateTime.Now,
       DateTime.Now.Subtract(requestStart),
       status));

A quick test on my machine using Firefox and connecting to the built-in Visual Studio Web Server showed that two requests were executed simultaneously on the client and handled by two different threads on the server.

使用Firefox并连接到内置的Visual Studio Web Server对我的计算机进行快速测试表明,两个请求在客户端上同时执行,并由服务器上的两个不同线程处理。

As far as I know, there are a limited number of allowed concurrent requests allowed by the browser. Here is another post that on SO that covers what each value is for each of the popular browsers: How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

据我所知,浏览器允许的允许并发请求数量有限。这是SO上的另一篇文章,涵盖了每个流行浏览器的每个值:在流行的浏览器中允许多少并发AJAX(XmlHttpRequest)请求?

EDIT: I reread your post and saw that you mentioned IIS 6. I do not have access to IIS 6, but running it on an IIS 7 Web Server with a 5 second artificial timeout (Thread.Sleep) showed that multiple requests were being handled at the same time by different server threads. None of them start at the EXACT same time, so there might be some queuing going on at the server, but with a 5 second delay it's easy to see that in Firefox 3.0 I have at least 6 concurrent requests at a time.

编辑:我重读你的帖子,看到你提到了IIS 6.我没有访问IIS 6,但在IIS 7 Web服务器上运行它与5秒的人工超时(Thread.Sleep)显示正在处理多个请求同时由不同的服务器线程。它们都没有在同一时间开始,所以在服务器上可能会有一些排队,但是延迟5秒,很容易看出在Firefox 3.0中我一次至少有6个并发请求。

#3


You are calling your ajax request every time you find an element with the class .fedex_status.

每次找到类.fedex_status的元素时,都在调用ajax请求。

Your line

$(".fedex_status").each

is saying do this everytime you find an element with the class .fedex_status.

每当你找到一个类为.fedex_status的元素时,就是说这样做。

#1


In theory, ASP.NET with Session state enabled locks the session for each user request, thus processing them serially. I don't think you can disable Session for an MVC project painlessly, because TempData for example uses session by default. But you can try.

理论上,启用了会话状态的ASP.NET会锁定每个用户请求的会话,从而以串行方式处理它们。我不认为你可以无痛地禁用MVC项目的Session,因为TempData例如默认使用session。但你可以试试。

EDIT: here's a related question

编辑:这是一个相关的问题

#2


Your code works as expected on my machine. I had to insert any artificial delay with Thread.Sleep to see it in action though because the unadulterated requests executed so fast that it appeared they were all completed at once.

您的代码在我的机器上按预期工作。我不得不使用Thread.Sleep插入任何人工延迟来查看它的运行情况,因为未执行的请求执行得如此之快,以至于它们似乎都立即完成了。

 System.Threading.Thread.Sleep(1000);

 return Content(String.Format("Thread #{0}, Started = {1:hh:mm:ss.FFF}, Completed = {2:hh:mm:ss.FFF}, Duration = {3:hh:mm:ss.FFF}, Result = {4}",
       System.Threading.Thread.CurrentThread.ManagedThreadId,
       requestStart,
       DateTime.Now,
       DateTime.Now.Subtract(requestStart),
       status));

A quick test on my machine using Firefox and connecting to the built-in Visual Studio Web Server showed that two requests were executed simultaneously on the client and handled by two different threads on the server.

使用Firefox并连接到内置的Visual Studio Web Server对我的计算机进行快速测试表明,两个请求在客户端上同时执行,并由服务器上的两个不同线程处理。

As far as I know, there are a limited number of allowed concurrent requests allowed by the browser. Here is another post that on SO that covers what each value is for each of the popular browsers: How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

据我所知,浏览器允许的允许并发请求数量有限。这是SO上的另一篇文章,涵盖了每个流行浏览器的每个值:在流行的浏览器中允许多少并发AJAX(XmlHttpRequest)请求?

EDIT: I reread your post and saw that you mentioned IIS 6. I do not have access to IIS 6, but running it on an IIS 7 Web Server with a 5 second artificial timeout (Thread.Sleep) showed that multiple requests were being handled at the same time by different server threads. None of them start at the EXACT same time, so there might be some queuing going on at the server, but with a 5 second delay it's easy to see that in Firefox 3.0 I have at least 6 concurrent requests at a time.

编辑:我重读你的帖子,看到你提到了IIS 6.我没有访问IIS 6,但在IIS 7 Web服务器上运行它与5秒的人工超时(Thread.Sleep)显示正在处理多个请求同时由不同的服务器线程。它们都没有在同一时间开始,所以在服务器上可能会有一些排队,但是延迟5秒,很容易看出在Firefox 3.0中我一次至少有6个并发请求。

#3


You are calling your ajax request every time you find an element with the class .fedex_status.

每次找到类.fedex_status的元素时,都在调用ajax请求。

Your line

$(".fedex_status").each

is saying do this everytime you find an element with the class .fedex_status.

每当你找到一个类为.fedex_status的元素时,就是说这样做。