问题描述
摘要:在库的方法,我应该使用异步
和的await
关键字而不是返回工作
直接?
我相信我的问题是有关this 之一。然而,问题是关于 .NET 4.0
和TPL,而我使用.NET 4.6的异步
和等待
关键字。所以,我想是因为当链接问题得到回答不存在这些关键字,我的问题可能会得到不同的答案。
说明:我在写一个外部WCF服务的一个简单的包装和包装,使多个 SendAsync
来电。现在我认为,每个包装方法应该只返回一个任务<>
的情况下直接被期待已久的。我的理解是,异步
/ 伺机
应在应用层上使用,而且的不的库中。
因此,举例来说,这里要说的是,我想我应该为每个包装方法的办法:
私人任务< SignResponse> GetSignDataAsync(SigningRequestType要求)
{
返回_service.SendAsync(请求);
}
但在互联网上,我发现的几个职位使用这种方法来代替:
专用异步任务< SignResponse> GetSignDataAsync(SigningRequestType要求)
{
返回等待_service.SendAsync(要求).ConfigureAwait(假);
}
这是另外一个例子,我发现 TechNet上:
异步任务PutTaskDelay()
{
等待Task.Delay(5000);
}私人异步无效btnTaskDelay_Click(对象发件人,EventArgs的发送)
{
等待PutTaskDelay();
的MessageBox.show(我回来了);
}
所以,当我应该使用第二种方法(即包含一个异步
和等待
关键字)?为什么不直接返回一个整体工作
未做 PutTaskDelay
异步
?我想我应该回到工作
直接每当它是可能的,并使用异步
/ 等待
来得到仅在应用层的最终结果。我对吗?如果不是,那我在这里展示的两种方法之间的区别是什么呢?
我的关心:当异步
和等待
使用关键字,似乎它只是提供了额外的工作,编译器没有任何好处。
It all depends. If you're going to take advantage of the asynchronous programming paradigm, then the answer is "yes," the async
and await
keywords are needed most of the time. More than likely, you will find yourself needing to use async/await
. That is because in most situations it would be difficult to use only Task
and Task<T>
as you will more than likely need to reason about the results of the async operations that you invoke.
Additionally, based on your question it seems as though you may have some confusion about the keywords themselves and how they relate to the Task
and Task<T>
types. Allow me to clarify this for you.
The async
keyword allows a method to use the await
keyword. The best practice is to have all async methods return either Task
or Task<T>
unless you are unable to (for example, a button click event handler as you demonstrated above).
Methods that return Task
or Task<T>
represent asynchronous operations. When you are in a library it is suggested to always use .ConfigureAwait(false)
for reasons detailed here. Additionally, I always point people to this detailed article on the subject.
To differentiate the two approaches in your question:
The method below returns a Task<SignResponse>
. This is an async operation that represents the work to sign in. The method can be awaited by the caller to get SignResponse
.
private Task<SignResponse> GetSignDataAsync(SigningRequestType request)
{
return _service.SignAsync(request);
}
Likewise, this version does the same thing...except that the async/await
keywords are not needed. The reason they are not needed is that the method itself does not need to use SignResponse
and therefore it could simply return Task<SignResponse>
as shown above. And as you indicated in your question, there is indeed a penalty when you use the async/await
keywords when they are not needed. Doing so adds an extra state machine step as the result is yielded, since its awaited.
private async Task<SignResponse> GetSignDataAsync(SigningRequestType request)
{
return await _service.SignAsync(request).ConfigureAwait(false);
}
Finally, if you needed to reason about the response, you could use the aforementioned keywords to do so like this:
private async Task<SignResponse> GetSignDataAsync(SigningRequestType request)
{
var result = await _service.SignAsync(request).ConfigureAwait(false);
if (result.SomeProperty == SomethingWeCareToCheck)
{
_log.Log("Wow, this was un-expected...");
}
return result;
}
这篇关于始终使用“异步”,并在库异步方法'等待'关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!