MVC 4 ApiController和异步并行 [英] MVC 4 ApiController and async concurrency

查看:855
本文介绍了MVC 4 ApiController和异步并行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩在ApiController异步方法和行为让我吃惊。我调用下面的方法:

  //获取API /价值/ {ID}
公共异步任务<对象>获取(INT ID)
{
    VAR收到= DateTime.Now;
    等待Task.Delay(10000);
    VAR完成= DateTime.Now;
    返回新{ID,接收完};
}
 

这四款浏览器窗口与以下列网址,提供有关各一间秒钟的延迟:

 的http://本地主机:55571 / API /价值/ 1
HTTP://本地主机:55571 / API /价值/ 2
HTTP://本地主机:55571 / API /价值/ 3
HTTP://本地主机:55571 / API /价值/ 2
 

第一个URL回来,如预期的,10秒我启动它后,在此之后是第二和第三的URL不久。然而,第四URL,它是相同的第二个URL,第二个URL返回之后返回10秒。

这表明,我认为浏览器和我的code之间的东西是连载的请求到相同的URL,或同一组参数,还是其他什么东西(和什么有关POST,PUT,等?)。而不是反向工程,我想问问...

这是行为记录的地方?

编辑:我试图抓着这根救命稻草,都无济于事。

编辑:作为要求在评论,定时为(大约)如下:

  0秒:GET ... /价值/ 1
 1秒:GET ... /价值/ 2
 2秒:GET ... /价值/ 3
 3秒:GET ... /价值/ 2(第二个实例)
10秒:GOT ... /价值/ 1
11 S:GOT ... /价值/ 2
12秒:GOT ... /价值/ 3
21 S:GOT ... /价值/ 2(第二个实例)
 

解决方案

浏览器会自动限制连接他们到主机的数量。 2616 (HTTP 1.1)建议的两个极限持久连接;它出现在你的浏览器使用三个的限制。

I'm playing with async methods in an ApiController, and the behaviour surprises me. I invoke the following method:

// GET api/values/{id}
public async Task<object> Get(int id)
{
    var received = DateTime.Now;
    await Task.Delay(10000);
    var finished = DateTime.Now;
    return new { id, received, finished };
}

from four browser windows with the following URLs in order, with about a one second delay between each:

http://localhost:55571/api/values/1
http://localhost:55571/api/values/2
http://localhost:55571/api/values/3
http://localhost:55571/api/values/2

The first URL comes back, as expected, ten seconds after I launch it, followed shortly thereafter by the second and third URLs. However, the fourth URL, which is identical to the second URL, returns ten seconds after the second URL returns.

This suggests to me that something between the browser and my code is serialising requests either to the same URL, or the same set of parameters, or something else (and what about POST, PUT, etc.?). Rather than reverse-engineering this, I'd like to ask...

Is this behaviour documented somewhere?

EDIT: I tried clutching at this straw, to no avail.

EDIT: As requested in a comment, the timings are (roughly) as follows:

 0 s: GET …/values/1
 1 s: GET …/values/2
 2 s: GET …/values/3
 3 s: GET …/values/2 (2nd instance)
10 s: GOT …/values/1
11 s: GOT …/values/2
12 s: GOT …/values/3
21 s: GOT …/values/2 (2nd instance)

解决方案

Browsers will voluntarily limit the number of connections they have to a host. RFC 2616 (HTTP 1.1) recommends a limit of two persistent connections; it appears your browser is using a limit of three.

这篇关于MVC 4 ApiController和异步并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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