所有实体框架方法都应该使用异步吗?

Should all Entity Framework methods use async?(所有实体框架方法都应该使用异步吗?)

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

问题描述

在 Asp.Net MVC 或 Asp.Net Web API 中,让查询数据库的每个控制器操作(即使是最简单的查询)都使用异步/等待模式是一种好习惯吗?

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?

推荐答案

Entity Framework 使用数据库,需要访问数据库服务器.使用 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 bound 是指完成所需时间的条件计算主要取决于等待所花费的时间用于完成输入/输出操作.来源:维基百科

I/O bound refers to a condition in which the time it takes to complete a computation is determined principally by the period spent waiting for input/output operations to be completed. Source : Wikipedia

一些精度:

每个 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 操作使用异步/等待模式,则每个 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 模式.即使您的应用程序不是较大的应用程序,也始终建议使用 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.

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

本文标题为:所有实体框架方法都应该使用异步吗?

基础教程推荐