question

ANB avatar image
0 Votes"
ANB asked XiaopoYang-MSFT answered

Asynchronous Methods

I am trying to convert my methods to asynchronous.
I have added async Task<IActionResult> and seems the methods work fine.
However, just it is enough to make them asynchronous? Wouldn't I need an "await" somewhere? but where ?

// CONTROLLER
public async Task<IActionResult> GetProducts()
{
try
{
return Ok(_productServices.GetAllProducts());
}
catch (Exception ex)
{
return StatusCode(500, "");
}
}


// SERVICES
public IEnumerable<ProductModel> GetAllProducts()
{
var products = List<ProductModel>() {
...
}
return products
}

Thank you,

windows-api
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
0 Votes"
SimpleSamples answered SimpleSamples edited

Yes you need an await somewhere. More important to that is it helps to understand that neither await nor async creates tasks, they support multitasking. You need to do something (else) that creates a task.

Many developers prefer await and async but in my opinion they are unnecessarily complicated. You can do multitasking without them.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered

If GetProducts don't call await, It‘s unnecessary to claim async.
Only Task and async functions need to await. async and await.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.