简体   繁体   English

为什么我对web.api的请求被长时间运行的控制器代码阻止了?

[英]Why are my requests to web.api being blocked by long running controller code?

I'm working on my local development environment using angular to issue ajax calls to Web.API (IIS 8.5 hosted). 我正在使用angular向Web.API(托管IIS 8.5)发出Ajax调用的本地开发环境。 I'm making 5 calls via different angular controllers simultaneously, each of which look like this: 我正在通过不同的角度控制器同时拨打5个电话,每个电话看起来都像这样:

$scope.medications = API.Client.MedicationProfiles.query({ clientId: clientId });

The API service has this definition for the Client.MedicationProfiles resource: API服务对Client.MedicationProfiles资源具有以下定义:

this.Client.MedicationProfiles = $resource(
    '/Noteable.API/api/Client/:clientId/MedicationProfiles/:id',
    {
        clientId: '@clientId',
        id: '@Id'
    },
    {
        update: { method: 'PUT' }
    }
);

I can see all of the requests going to the server basically at once using the chrome dev tools as the page loads but it appeared that they were coming back one at a time as if they had been run synchronously. 当页面加载时,我基本上可以看到所有使用chrome dev工具一次发送到服务器的请求,但是看起来它们一次又一次返回,就好像它们是同步运行的一样。 I put a thread.sleep in one of the Web.API controller actions and sure enough, all calls issued after that one fail to return until the sleeping one does. 我在Web.API控制器操作之一中放入了一个thread.sleep,很确定,在那之后发出的所有调用都无法返回,直到正在休眠的调用才返回。 This doesn't make any sense to me and I'm wondering if someone can tell me in what case they would expect this. 这对我来说没有任何意义,我想知道是否有人可以告诉我在什么情况下会发生这种情况。

This may be a good situation to implement an asynchronous pattern on your web api backend - you'll be alleviated from your ajax calls blocking once they hit your controller. 这可能是在Web api后端上实现异步模式的一个好情况-一旦ajax调用碰到了控制器,就可以避免阻塞。 This can be achieved by leveraging an async and await pattern. 这可以通过利用asyncawait模式来实现。 You'll want to return a Task , which represents an asynchronous operation. 您将要返回一个Task ,它代表一个异步操作。

An example may include... 一个例子可能包括...

public async Task<IHttpActionResult> Put(string clientId)
{
    var response = await this.yourService.yourSavingMethod(clientId);

    return Ok(response);
}

Also see SO question Why is this web api controller not concurrent? 另请参阅SO问题为什么此Web api控制器为什么不并发? for more details and insight on the issue. 有关此问题的更多详细信息和见解。 Furthermore, the default behavior for ASP.NET Session State and can be changed to accommodate concurrent requests. 此外,ASP.NET会话状态的默认行为可以更改以适应并发请求。 An example may include 一个例子可能包括

HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.ReadOnly)

ASP.NET Session State Overview ASP.NET会话状态概述

I think your problem is caused either by browser or ASP.NET. 我认为您的问题是由浏览器或ASP.NET引起的。 In case it's ASP.NET, take a look at this thread: 如果是ASP.NET,请看以下线程:

C# Web API - Blocking C#Web API-阻止

In case it's your browser, take a look at this thread with example code: 如果是您的浏览器,请使用示例代码查看此线程:

Why is my async ASP.NET Web API controller blocking the main thread? 为什么我的异步ASP.NET Web API控制器阻塞了主线程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM