问题描述
大多数Web API 2.0方法我已经看到了回报 IHttpActionResult
,它被定义为定义了异步创建System.Net.Http.Htt命令接口presponseMessage。
Most Web API 2.0 methods I've seen return IHttpActionResult
, which is defined as an interface that "defines a command that asynchronously creates a System.Net.Http.HttpResponseMessage".
我有点困惑是怎么回事时,方法返回的异步任务< IHttpActionResult方式>
I'm a little confused about what's going on when a method is returning async Task<IHttpActionResult>
.
你为什么要使用一个比其他?或者是这些功能相同 - 不 IHttpActionResult
已异步
Why would you use one over the other? Or are these functionally identical - isn't IHttpActionResult
already asynchronous?
推荐答案
两者的区别: IHttpActionResult
和异步任务&LT; IHttpActionResult&GT;
为您的任何code是否利用异步
和的await
功能。像实体框架许多图书馆提供的方法异步
版本(例如 SaveChangesAsync
),提供了一个轻微的性能提升。不过,也有陷阱,使用异步使用Web API,所以除非你明白了许多特质的谨慎的做法是坚持同步API。
The difference between using IHttpActionResult
and async Task<IHttpActionResult>
is whether any of your code utilizes the async
and await
feature. Many libraries like Entity Framework provide async
versions of methods (e.g. SaveChangesAsync
) that provide a slight performance increase. However, there are pitfalls to using async
with Web API, so unless you understand many of the idiosyncrasies it is prudent to stick to the synchronous API.
史蒂芬克利对 的异步特质
和等待
。要开始我建议你看一看。
Steven Cleary has a lot of information on his blog about the idiosyncrasies of async
and await
. To get started I advise looking at Don't block on async code.
这篇关于IHttpActionResult VS异步任务&LT; IHttpActionResult&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!