Summary: In a library method, when should I use the async
and await
keywords instead of returning a Task
directly?
I believe my question is related to this one. However, that question is about .NET 4.0
and the TPL, while I'm using .NET 4.6 with the async
and await
keywords. So, I think my question might get different answers because these keywords didn't exist when the linked question was answered.
Explanation: I'm writing a simple wrapper for an external WCF service and the wrapper makes multiple SendAsync
calls. Now I think that each wrapper method should just return a Task<>
directly without being awaited. My understanding is that async
/await
should be used on the application layer, and not within a library.
So, for example, here is the approach that I think I should take for each wrapper method:
private Task<SignResponse> GetSignDataAsync(SigningRequestType request)
{
return _service.SendAsync(request);
}
But on the Internet, I found several posts that use this approach instead:
private async Task<SignResponse> GetSignDataAsync(SigningRequestType request)
{
return await _service.SendAsync(request).ConfigureAwait(false);
}
And here is another example that I found on technet:
async Task PutTaskDelay()
{
await Task.Delay(5000);
}
private async void btnTaskDelay_Click(object sender, EventArgs e)
{
await PutTaskDelay();
MessageBox.Show("I am back");
}
So, when should I use the second approach (the one that includes the async
and await
keywords)? Why not just return a whole Task
without making PutTaskDelay
async
? I think that I should return Task
directly whenever it is possible, and use async
/await
to get a final result in the application layer only. Am I right? If not, what is the difference between the two approaches that I show here?
My concern: When the async
and await
keywords are used, it seems that it just provides additional work to the compiler without any benefit.
async
/await
are implementation details of your methods. It matters not one jot whether your method is declaredasync Task Method()
or justTask Method()
, so far as your callers are concerned. (In fact, you are free to change between these two at a later point in time without it being considered a breaking change) – PhenomenaSignAsync
toSendAsync
. I hope that was what you intended. – FarceSignAsync
I meantasync Sign
method, because I called it to get a digital signature for byte array in some certification center. But I won't edit this post becuase method nameSign
orSend
doesn't matter. I consider that intent is actually preserved, so I just thank you for a good job :) – DysplasiaSendAsync
calls." So, I was a bit confused when I sawSignAsync
in your code examples. Also, after quickly checking MSDN, the versions ofSendAsync
andSignAsync
that I looked at both take more than one parameter. So, if you think you can make the code examples more realistic, I encourage you to do so. That's because some people might arrive at your question looking forSendAsync
orSignAsync
. – FarceSendAsync
is fine, because as I said, specific method name doesn't matter in this case. – DysplasiaSendAsync
orSignAsync
isn't central to your question. Our comments should clear up any confusion that someone might have if they arrive here looking forSendAsync
orSignAsync
. – Farce