0

I have a ASP.NET WEB API controller with a method like this:

[HttpGet]
[Route("some/route/to/endpoint")]
public Task<Data> DoStuffAsync()
{
    var somevar = Guid.NewGuid();
    return DoSomeInternalStuffAsync();
}

private async Task<Data> DoSomeInternalStuffAsync()
{
    var result = await SomeReallyInternalThingsAsync();
    return result.Data;
}

What is the difference on eliding the async/await and to actually use it like:

[HttpGet]
[Route("some/route/to/endpoint")]
public async Task<Data> DoStuffAsync()
{
    var somevar = Guid.NewGuid();
    return await DoSomeInternalStuffAsync();
}

I read a lot of articles and Stackoverflow posts, but it is still unclear to me:

  1. What is the internal handling of a controller method returning a Task VS returning an awaited task?
  2. Will the controller method be awaited in each case or is returning a Task on a controller method a pure synchronous operation?

Articles read so far: Eliding async await, Asynchronous Programming, Stackoverflow Thread 1, Stackoverflow Thread 2

Update: My questions are answered in the comments.

afrischk
  • 348
  • 3
  • 11
  • 1
    You asked Camilo for a reference. I found the [ASP.NET code](https://github.com/aspnet/Mvc/blob/a67d9363e22be8ef63a1a62539991e1da3a6e30e/src/Microsoft.AspNetCore.Mvc.Core/Internal/ActionMethodExecutor.cs#L110-L190) that resolves the response. – ProgrammingLlama Aug 16 '18 at 14:47
  • 3
    I see a comment on a deleted answer where you ask " So the framework awaits regardless of the method being async". Consider this - you're not allowed `async` in interface definitions. And if you're overriding a `Task` returning method, the base class, the derived class, both or neither can decorate their implementation with `async`. It's a *implementation* detail of the method it decorates (hence why not allowed on interfaces), it just appears in a funny place that makes it look like it's part of the signature of the method. – Damien_The_Unbeliever Aug 16 '18 at 14:48
  • @John Thanks, so Tasks from controllers are awaited. That made it crystal clear to me. – afrischk Aug 16 '18 at 14:56
  • @Damien_The_Unbeliever Thanks, this was also valuable. – afrischk Aug 16 '18 at 14:57

0 Answers0