0

Hi I am new to async/await in C# . I have created a controller which is accessing result from HttpClient injected through HttpFactory. Here is my working example

class MyController
{

     private readonly IExternalServiceManager _externalServiceManager;
     public MyController(IExternalServiceManager externalServiceManager)
     {
           _externalServiceManager=externalServiceManager;
     }
     
     [Httpget]
     [Route("getemployeedetails/{employeeId}")]
     public async Task<IHttpActionResult> GetProjects(int employeeId)
     {
            var results=await _externalServiceManager.GetProjectsForEmployee(employeeId);
            return Ok(results)
     }


}

interface IExternalServiceManager
{
       Task<List<string>> GetProjectsForEmployee(int employeeId);
 
}
 
internal class ExternalServiceManager:IExternalServiceManager
{
     private readonly HttpClient _httpClient;

     public ExternalServiceManager(HttpClient httpClient)
     {
        _httpClient = httpClient;
     }

     public async Task<List<string>> GetProjectsForEmployee(int employeeId)
     {
           HttpResponseMessage response = await _httpClient.GetAsync("http://someuri");
           if(response.IsSuccessStatusCode)
           {
              string strResponse = await response.Content.ReadAsStringAsync();
              return JsonConvert.DeserializeObject<List<string>>(strResponse);
           }
     }
}

I have added a lot of await keywords here, and I am confused which await should I remove. Or is there a better way to restructure this code ?

I also added an extension method to avoid duplicating code

public async static Task<T> ReadAndDeserializeFromAsync<T>(string endPointURL)
{
     HttpResponseMessage response = await httpclient.GetAsync(endpointURL);
           if(response.IsSuccessStatusCode)
           {
              string strResponse = await response.Content.ReadAsStringAsync();
              return JsonConvert.DeserializeObject<T>(strResponse);
           }

}

    public async Task<List<string>> GetProjectsForEmployee(int employeeId)
    {
           var results = await _httpClient.ReadAndDeserializeFrom<List<String>>("https"//someURL").ConfigureAwait(false);
           return results;
    }

Does using too many await degrade performance ?

7
  • Why do you feel that you have too many awaits? – Nathanael Dec 30 '20 at 16:39
  • Does every await has performance affect ? should I use only one await? – Rohit Dec 30 '20 at 17:10
  • 1
    I wouldn't worry about the number of await keywords. That's like worrying you're using return too much. I don't see any problems in your code. – John Wu Dec 30 '20 at 17:51
  • 1
    You might also be interested in this article: Eliding Async and Await, which touches on the performance impact of using an await. – John Wu Dec 30 '20 at 17:53
  • 1
    just return _httpClient.ReadAndDeserializeFrom....... – Ewan Jan 1 at 14:06
1

Using await is a normal part of using async in C#. Await tells your code that it needs the result of the task before proceeding. It sounds to me you may not understand how async/await works or what it does. I highly recommend anything on the topic by Stephen Clearly to better understand the concepts.

To directly answer your question, there is some overhead in using the async framework in C#. However, if the operation you are doing is truly asynchronous, you should see better performance in your application. Sometimes dramatically so.

2
  • The performance benefit would be measured over many operations, not just a single operation. An async call does not execute faster. It just allows that one operation to be suspended while it waits on some external resource, so the machine can do other work in the meantime. It's the "other work in the meantime" that garners the performance boost. – Greg Burghardt Dec 30 '20 at 18:04
  • @GregBurghardt I understand that completely. I was just giving a general answer. – Nathanael Dec 30 '20 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.