2

We have Application service Method, which extracts data from SQL database using Entity Framework Core . Its Async method along with the Async Controller.

Service:

public async Task<IEnumerable<ProductType>> GetProductTypeData()
{
    var result = _dbContext.Set<ProductType>()
        .Where(x => x.LkProcessActionTypeId > 0 && x.Status.HasValue && x.Status.Value).AsNoTracking()
        .ToListAsync();
    return await result; 
}

API Controller:

[HttpGet("[Action]")]
public async Task<ActionResult<ProductType>> GetProductTypeAPI()
{
    var response = await productService.GetProductTypeData();
    return Ok(response);
}

Someone mentioned, its better practice to make Service method just Task, no Async (to avoid Double Async) Would there be any difference between, using Async or No Async in the application method?

The Service Method does not necessarily have to be utilized with the API.

New Application Method (No Async)

public Task<IEnumerable<ProductType>> GetProductTypeDto()
{
    var result = _dbContext.Set<ProductType>()
        .Where(x => x.LkProcessActionTypeId > 0 && x.Status.HasValue && x.Status.Value).AsNoTracking()
        .ToListAsync();
}

Resource: This does not directly answer the question,

https://stackoverflow.com/questions/19098143/what-is-the-purpose-of-return-await-in-c

5
  • "its better practice to make Service method just Task, no Async (to avoid Double Async)" What reason did they give for this? Commented Aug 17, 2020 at 5:05
  • 2
    I have never heard of a "double await" being bad and honestly can't think of any reason to do so. You are as far as I can tell using Tasks exactly the way they were intended to be used.
    – Turksarama
    Commented Aug 17, 2020 at 7:33
  • 1
    Synchronous: Control is regained when the work is complete. The executed function is blocked until that time. Asynchronous: The control is returned immediately . Refer: docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/…
    – AjayGohil
    Commented Aug 17, 2020 at 10:49
  • 2
    Mandatory reading: Eliding Async and Await. TLDR: It barely matters performance-wise, but is slightly safer to double await, as it makes certain types of bugs less likely.
    – John Wu
    Commented Aug 18, 2020 at 23:24
  • Please check David Flower's (ASP.NET Architect @ Microsoft) Async guideline Commented Aug 25, 2020 at 15:02

2 Answers 2

-1

Every async method produces a small state machine, so for a method that only calls another method that returns a Task, simply returning the Task instead of awaiting it is slightly more efficient.

Be careful when adding additional code later on though, for example if you add a try-catch statement you have to await it or any exception won’t be caught.

-1

As mentioned earlier each async generates a state machine causes new Task to allocate. It's best practice go for just Task as much as you can But watch out for using it in conjunction with httpclient

public Task<HttpResponseMessage> TestRequestAsync()
{
     var response = default(HttpResponseMessage)
     using(var client = new HttpClient())
     {
          response = client.Post(...)
     }

     return response;
}

Do be aware in this case you'll end up with run time error, cause once you try to await it HttpClient would be already disposed.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.