IHttpActionResult VS异步任务< IHttpActionResult> [英] IHttpActionResult vs async Task<IHttpActionResult>

查看:898
本文介绍了IHttpActionResult VS异步任务< IHttpActionResult>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数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 ),提供了一个轻微的性能提升。不过,也有陷阱,使用<​​code>异步使用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.

史蒂芬克利对他的博客了解 的异步特质等待。要开始我建议你看一看不要阻止在异步code

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;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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