0

What is actually returned by a Web API method with the following signature?

    [HttpPost]
    public async Task<IHttpActionResult> Post([FromBody] ReviewViewModel review)
    {
        using (var context = new BooksContext())
        {
            var book = await context.Books.FirstOrDefaultAsync(b => b.Id == review.BookId);
            if (book == null)
            {
                return NotFound();
            }

            var newReview = context.Reviews.Add(new Review
            {
                BookId = book.Id,
                Description = review.Description,
                Rating = review.Rating
            });

            await context.SaveChangesAsync();
            return Ok(new ReviewViewModel(newReview));
        }
    }

Method taken from: http://www.developerhandbook.com/c-sharp/create-restful-api-authentication-using-web-api-jwt/

I'm considering that it is either:

1) The framework does not return a response to the calling client until .IsCompleted is true, or 2) The framework does return to the client but the client must handle the situation gracefully, or 3) Something else entirely.

Matt W
  • 11,753
  • 25
  • 118
  • 215

2 Answers2

5

async actions are an implementation detail.

The framework will wait for the Task to resolve; the client won't notice any difference.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

From the link you provided:

Writing asynchronous code in this manner allows the thread to be released whilst data (Books and Reviews) is being retrieved from the database and converted to objects to be consumed by our code. When the asynchronous operation is complete, the code picks up where it was up to and continues executing. (By which, we mean the hydrated data objects are passed to the underlying framework and converted to JSON/XML and returned to the client).

Mihail Stancescu
  • 4,088
  • 1
  • 16
  • 21