所有实体框架方法应该使用异步? [英] Should all Entity Framework methods use async?

查看:152
本文介绍了所有实体框架方法应该使用异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Asp.Net MVC或Asp.Net Web API中,好的做法是让每个控制器的动作查询数据库(即使是最简单的查询)来使用async / await模式?

Is it good practice, in Asp.Net MVC or Asp.Net Web API, to have every controller actions that query the database (even the simplest query) to use async/await pattern?

我知道使用 async / await 增加了复杂性,但是添加它值得吗?即使是最简单的查询?

I know using async/await adds complexity, but does adding it worth it? Even for the simplest query?

推荐答案

实体框架使用数据库,需要访问数据库服务器。使用EF,您需要连接数据库服务器,等待服务器响应您的请求。

Entity Framework uses database and need access to the database server. With EF you'll need to connect the databse server and wait for the server to respond to your request.

如果您的应用程序使用磁盘或网络(如访问数据库)读/写,那么它正在进行I / O操作。这是一个很好的做法,每个I / O操作都应该使用async / await模式,这是EF6暴露了许多可以使用的异步操作。

If your application uses disk or network (like access to a database) read/write then it is doing I/O operation. It's a good practice that every I/O operation should use async/await pattern that is what EF6 exposes many async operations that you can use.


I / O绑定是指完成
a计算所需的时间主要取决于所花费的时间等待
输入/输出操作完成。
资料来源:维基百科

有些细节:

每个ASP.Net Web API请求都使用由.Net Framework线程池提供的线程。如果您对ASP.Net Web API操作使用同步方法,因此I / O绑定操作(数据库访问)将阻止线程并等待数据库响应。您的请求使用的线程将被阻止,不会返回到线程池。

Each ASP.Net Web API request use a thread that are given by .Net Framework thread pool. If you use synchronous method for ASP.Net Web API actions so the I/O bound operation (database access) will block the thread and wait for the database to respond. The thread used by your request will be blocked and not returned to the thread pool.

胎面游泳池使用的最大线程为5000(.Net 4.5)。如果您的应用程序是一个可以快速达到最大值的大型应用程序。如果线程池中没有线程可用,那么新的请求将被添加到队列中。如果您的服务器队列已满,它将拒绝具有 HTTP 503 状态的请求,代表服务器太忙

The maximum thread that are used by the tread pool is 5000 (.Net 4.5). If your application is a large application that maximum can be reached rapidly. If no thread is available in the thread pool so new requests will be added to the queue. If your server queue becomes full, it will reject requests with an HTTP 503 status whic stands for "Server Too Busy".

如果您的ASP.Net Web API操作正在使用async / await模式,那么每个I / O绑定操作将释放当前请求的线程。该线程可以被另一个请求使用。如果I / O绑定操作完成任务,则另一个线程将被处理ASP.Net Web API的其余操作方法。

If your ASP.Net Web API actions are using async/await pattern then each I/O bound operation will free the current request's thread. This thread can be used by another request. If the I/O bound operation finished its task then another thread is given to process the rest of your ASP.Net Web API action method.

所以要回答你的问题。您的ASP.Net Web API需要访问数据库的每一个动作都应该使用async / await模式,如果你的应用程序可以有很多的并发性。即使您的应用程序不是更大的应用程序,始终建议使用异步/等待I / O绑定操作。

So to answer to your quesiton. Every action of your ASP.Net Web API that need access to your database should use async/await pattern if your application can have a lot of concurrency. Even if your applicaiton is not a larger application it is always recommended to use async/await for I/O bound operations.

您可以检查这个文章。它讨论ASP.Net MVC的使用异步方法,但大多数建议可以用于ASP.Net Web API。

You can check this article. It talk about "Using Asynchronous Methods" for ASP.Net MVC but the majority of the recommendations can be used for ASP.Net Web API.

这篇关于所有实体框架方法应该使用异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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