我目前正在玩一个异步的WebAPI控制器,服务图像,我面临一个奇怪的行为。异步WebAPI控制器服务图像 - 挂起的HTTP请求
简短情况是: Controller通过ID获取Image并从数据库通过EF6加载它。 blobresult包含文件名,contentType和数据为byte []。 我有潜在100的小图像画廊指向通过标准img标记控制器是这样的:
<img src="//path/to/webapicontroller/1" />
这是或多或少的完整代码:
public class BlobImageController : ApiController
{
public async Task<HttpResponseMessage> Get(Guid id, [FromUri]int width, [FromUri]int height)
{
var service = new BlobService();
// pull stuff via EF from database using async EF APIs
var blobResult = await service.ReadAsync(id);
string contentType = blobResult.ContentType;
var fileExtension = Path.GetExtension(blobResult.Filename);
var result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(blobREsult.content);
stream.Position = 0;
result.Content = new StreamContent(stream);
result.Content.Headers.ContentLength = stream.Length;
result.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
return result;
}
}
问题: 之前的同步控制器服务的很好,但是这个异步版本的浏览器只下载前7-10张图片,所有其他请求都在等待处理。在服务器端没有负载 - 它卡在某个地方。 如果我使用其他浏览器,它也可以与前7一起使用,并且我只能在服务器端收到7个请求。
当我看着Chrome开发工具时,未完成的请求全部挂起,而成功的请求是“OK”。
如果我使用ByteArrayContent,也会发生这种情况。这两个版本都与同步版本配合良好。
我错过了什么?
你在.NET 4.5,并有'targetFramework'设置为'4.5'在您的网页。配置? –
是 - config&project中的targetFramework =“4.5”。 webapp甚至依赖于一些.NET 4.5程序集。 –