C#的Web API - 阻止 [英] C# Web API - Blocking

查看:233
本文介绍了C#的Web API - 阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经建立了从外部API加载数据的Web服务。一个标准的页面包含6-8个模块,从不同的API资源的所有加载数据。它看起来像这样(从不同的URL每个块的负载,例如/车/电话/游戏...

We have built a web service that loads data from an external API. A standard page contains 6-8 modules, all loading data from different resources from the API. It looks like this(each block loads from a different url, e.g. /cars, /phones, /games...

一个调用外部API可以采取像10-15秒(不寻常的全部)...

One call to the external API can take like 10-15 second(not unusual at all)...

在加载页面的每个块将触发一个Ajax调用使用jQuery(我们使用的骨干),要求我们自己的API(它就像一个代理,只将呼叫路由着外部API)数据。

When loading the page each block will fire an Ajax call with jQuery(we are using backbone) that asks our own API(it's like a proxy, only routes the call forward to the external API) for data.

这是我们认为不错啊,阿贾克斯将解决我们所有的问题......和它,在客户端开始......不过,我们不使用异步或类似的东西在服务器端,所以我想你可以看到,我们正面临着什么样的问题......这是正确的,阻塞线程。

From the beginning we thought "Ah nice, ajax will solve all our problems"...and it does, on the client side...However, we are not using async or anything like that on the server side so I guess you can see what problems we are facing...that's right, blocking threads.

我们不能有超过2-3并发用户因为这个原因。
我们使用C#MVC连同网页API并在前端,我们使用的是骨干。

We cant have more than 2-3 concurrent users because of this. We are using c# MVC together with Web Api and on the frontend we are using Backbone.

我的问题是;会异步控制器解决我们的问题?或者我们应该以另一种方式实现这一点?你们当中有些人会说只是缓存数据,你会被罚款。是的,我们正在缓存的数据,但它仍然会堵塞所有线程(只要不是第一个请求,但仍明显)

My question is; will a async controllers solve our problems? Or should we implement this in another way? Some of you will say "just cache the data and you will be fine". And yes, we are caching the data but it will still block up all the threads(not as long as the first request, but still noticeable)

我看到这个好几个不同的解决方案:

I see quite a few different solutions for this:


  • 反向AJAX - 如 http://pokein.com/

  • SignalR? http://www.asp.net/signalr

  • 异步控制器(使用异步方法获取那当然的数据......)

  • 派生为每个请求一个新的线程(不会阻塞主线程...我想这不是一个很好的解决方案的话)?

那么,你们怎么推荐?

另外,我发现这一点:<一href=\"http://stackoverflow.com/questions/14996529/why-is-my-async-asp-net-web-api-controller-blocking-the-main-thread\">Why是阻塞主线程异步我的ASP.NET Web API控制器?
如果回答表明它是与浏览器本身的问题,它不从第一次请求的响应之前允许多个并发呼叫?

Also, I found this: Why is my async ASP.NET Web API controller blocking the main thread? Where the answer suggesting that it's a problem with the browser itself, that it doesn't allow multiple concurrent calls before getting a response from the first request?

推荐答案

解决方案是非常简单的。在我basecontroller,我把这个属性:
SessionState会(SessionStateBehavior.Disabled)

The solution was really simple. On my basecontroller, I put this attribute: SessionState(SessionStateBehavior.Disabled)

ASP.NET把所有的请求的锁,这意味着如果你做的并发请求,他们不会并行运行。

ASP.NET puts a lock on all requests, which means if you do concurrent requests, they will not run in parallel.

您可以阅读更多关于它在本文来自微软

You can read more about it in this article from Microsoft.

访问ASP.NET会话状态是每个会话排斥的,这意味着
  如果两个不同的用户作出并发请求,获得每
  单独的会话同时授予。然而,如果两个并发
  请求在同一会议上提出的(使用相同的SessionID
  值),第一个请求到达会话独占访问
  信息。第二个要求只有第一个请求后执行
  结束了。 (第二次会议也可以得到,如果独占访问
  锁定的信息,因为第一个请求超出被释放
  锁定超时。)

Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.)

这篇关于C#的Web API - 阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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