在带有EF的ASP.Net WebAPI方法中使用Async有什么好处? [英] What's the benefit with using Async in an ASP.Net WebAPI method with EF?

查看:373
本文介绍了在带有EF的ASP.Net WebAPI方法中使用Async有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了一个问题,并给了两个答案:

I recently asked a question and was given two answers:

The sync version:

var phrasesCount = db.Phrases.Count();
The async version (assuming this is EF):

var phrasesCount = await db.Phrases.CountAsync();

有人可以告诉我有什么区别,为什么我要使用异步?

Can someone explain to me what the difference is and why I might want to use Async?

推荐答案

正在运行您的应用的工作进程具有数量有限的线程可用于处理http请求。目的是使这些线程保持空闲状态,以便它们可用于处理传入的请求。异步版本不会阻止调用线程。这使该线程可以返回处理那些传入的http请求。同时,异步方法已被触发,完成后,将在 await 操作中从中断处继续执行。如果您正在运行应用程序并使用示例测试两者之间的差异,那么性能之间不会有太大差异。真正的价值在于您的应用收到的请求多于可用线程来处理它们。

The worker process where your app is running has a limited number of threads available to handle http requests. The goal is to keep those threads free so that they are available to handle incoming requests. The async version does not block the calling thread. This allows that thread to get back to handling those incoming http requests. In the meantime, the async method has been fired off and when it completes, execution will pick up where it left off at the await operation. If you are running your app and testing the difference between the two using your example, you will not see much of a difference between performance. The real value comes when your app has more requests incoming than there are threads available to process them.

这篇关于在带有EF的ASP.Net WebAPI方法中使用Async有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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