Battle Sparrow
Battle Sparrow

Reputation: 45

Is there a difference in Async execution vs non-async execution in WebAPI ASP.NET Core controllers?

Is there a difference between calling a method of service with async/await:

    [HttpPost]
    public async Task<SmthResponce> AddSmth([FromBody] SmthRequest smthRequest)
    {
        return await smthsService.AddSmthAsync(smthRequest);
    }

and without:

    [HttpPost]
    public Task<SmthResponce> AddSmth([FromBody] SmthRequest smthRequest)
    {
        return smthsService.AddSmthAsync(smthRequest);
    }

Upvotes: 1

Views: 727

Answers (1)

glenebob
glenebob

Reputation: 1973

From the callers perspective, there is no difference; however...

The first method (await) involves the creation of an additional Task, which will be completed when the inner Task completes.

The second method (returning the Task directly, when possible) is preferred.

Upvotes: 3

Related Questions