Reputation: 31
I am creating a asp.net web API project. This will be a logging service.
The applications will use this service by making async calls. It is mainly "fire and forget" principle. I don't want any thing from the service to be returned. Now my question is should I create web-API methods with 'async Task' OR 'async void' as return type.
I was referring to this Async/Await - Best practice.
Upvotes: 2
Views: 1864
Reputation: 1182
using System.Web.Http;
using AysncTask = System.Threading.Tasks.Task;
[HttpPut]
[Route("api/cleardata/{id}/{requestId}/")]
public async AysncTask ClearData(Guid id, Guid requestId)
{
try
{
await AysncTask.Run(() => DoClearData(id, requestId));
}
catch (Exception ex)
{
throw new Exception("Exception in ClearData", ex);
}
}
Upvotes: 1
Reputation: 456407
I don't want any thing from the service to be returned.
An HTTP WebAPI must return something, at least a status code. That's how HTTP works.
my question is should I create web-API methods with 'async Task' OR 'async void' as return type.
As described in the referenced article, you should avoid async void
. You should only use async void
for event handlers (or things that are logically event handlers). A controller action is not an event handler. Thus, you should use async Task
.
Upvotes: 4
Reputation: 1100
async Task of course - this make your server more scalable. Here J Richter explain why ( I think nobody can explain better).
Upvotes: 0