代码之家  ›  专栏  ›  技术社区  ›  anonymous

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

  •  1
  • anonymous  · 技术社区  · 17 年前

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

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

    因此,如果页面上有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"));
    }
    

    所有时间戳都不重叠。因此,控制器一次处理一个请求。这看起来糟透了。

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

    谢谢

    3 回复  |  直到 6 年前
        1
  •  5
  •   Community Mohan Dere    9 年前

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

    编辑:这里有一个 related question

        2
  •  1
  •   Community Mohan Dere    9 年前

     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));
    

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

    据我所知,浏览器允许的并发请求数量有限。下面是另一篇文章,介绍了每个流行浏览器的每个值: How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

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

        3
  •  0
  •   Rigobert Song    17 年前

    你的线路

    美元(“.fedex_状态”)。每个