问题描述
如果我想通过返回一个 Task
对象来编写一个非阻塞的 web api 操作,我可以使用或不使用 async
关键字来完成它:
使用异步
公共异步任务得到(){Func慢调用 = () =>{线程睡眠(2000);return Request.CreateResponse(HttpStatusCode.OK, "Hello world");};var task = Task.Factory.StartNew(slowCall);返回等待任务;}
不使用异步
public Task得到(){Func慢调用 = () =>{线程睡眠(2000);return Request.CreateResponse(HttpStatusCode.OK, "Hello world");};var task = Task.Factory.StartNew(slowCall);返回任务;}
它们都可以正常工作.但是,我在编写返回 Task
的 web api 操作时(在线和在书籍中)看到的每个示例都使用 async
关键字.当然,我明白这给了你更大的灵活性,因为它允许你控制你想要等待"什么和不等待"什么.但假设您的功能可以通过任何一种方式得到有效处理,
- 使用一种方法与另一种方法相比有什么优势吗?
- 我是否应该始终使用 async 关键字(如果是,为什么)?
- 还是无关紧要?
async
关键字允许您通过创建状态机在方法中使用 await
.如果您可以管理返回异步任务而不使用它,您可以继续并删除它,因为它有一些(非常小的)开销.但请记住,它仅在少数情况下有用.您的 return await
就是其中之一.
另一个区别是异常的处理方式.如果方法的同步部分出现异常并且标记为async
,则异常将存储在返回的任务中.如果没有关键字,异常将定期抛出.例如在这种情况下有很大的不同:
var task = Get();//没有异步的未处理异常尝试{var 结果 = 等待任务;//异步处理异常}抓住{}
我的建议是使用 async
关键字,即使您不是绝对需要*,因为大多数开发人员不了解其中的区别,并且优化中的价值几乎可以忽略不计.>
If I wanted to write a non-blocking web api action by returning a Task
object, I could do it with or without using the async
keyword as such:
Using async
public async Task<HttpResponseMessage> Get()
{
Func<HttpResponseMessage> slowCall = () =>
{
Thread.Sleep(2000);
return Request.CreateResponse(HttpStatusCode.OK, "Hello world");
};
var task = Task<HttpResponseMessage>.Factory.StartNew(slowCall);
return await task;
}
Without using async
public Task<HttpResponseMessage> Get()
{
Func<HttpResponseMessage> slowCall = () =>
{
Thread.Sleep(2000);
return Request.CreateResponse(HttpStatusCode.OK, "Hello world");
};
var task = Task<HttpResponseMessage>.Factory.StartNew(slowCall);
return task;
}
They both work properly. However, every example I've seen (online and in books) on writing a web api action that returns a Task
uses the async
keyword. Granted, I understand that gives you more flexibility as it allows you to control what you want to "await" on and what not. But assuming your functionality could be effectively handled either way,
- Is there any advantage to using one approach vs the other?
- Should I always use the async keyword (and if so why)?
- Or is it irrelevant?
The async
keyword allows you to use an await
in your method by creating a state machine. If you can manage returning an asynchronous task without using it you can go ahead and remove it because it has some (very small) overhead. Keep in mind though that it's only useful in a few cases. Your return await
is one of them.
Another difference is how exceptions are handled. If there's an exception in the synchronous part of the method and it's marked as async
the exception will be stored in the returned task. Without the keyword the exception would be thrown regularly. For example in this case there's a big difference:
var task = Get(); // unhandled exception without async
try
{
var result = await task; // handled exception with async
}
catch
{
}
My recommendation is to use the async
keyword even when you don't absolutely need to*, because most developers don't understand the difference and the value in the optimization is mostly negligible.
这篇关于在方法签名中使用 async 关键字返回 Web Api 端点中的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!