REST - HttpClient - Server, C#, with await or without // What are the advantages and disadvantages?

Markus Freitag 3,791 Reputation points
2023-10-19T18:08:54.48+00:00

Hello,

HttpResponseMessage response = client.GetAsync("Api/valuesGET").Result;

HttpResponseMessage response2 = await client.GetAsync("Api/valuesGET");

Do you ever have an example to see the difference?

Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-10-20T06:55:41.1+00:00

    Hi @Markus Freitag,

    It looks like you want to understand the difference between synchronous and asynchronous programming. As already mentioned, asynchronous allows multiple tasks to run simultaneously without blocking the main program. The pros and cons based on your question will determine their usage scenarios are different:

    1. Because asynchrony allows multiple operations to occur simultaneously, it can improve overall program concurrency and performance utilization.
    2. Synchronous code is generally easier to read and write because it follows a more linear flow of execution, making debugging code more straightforward than asynchronous code. This reflects the certain complexity of asynchronous code.

    And the code (network communication) you provided belongs to an I/O-Bound task, and it doesn't require maintaining a specific order of execution, so I think it will be more efficient to use asynchronous.

    Additionally, I think you can simply refer to this document to recognize CPU-bound and I/O-bound work, as well as examples of using asynchronous programming in other scenarios.

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. SurferOnWww 4,721 Reputation points
    2023-10-20T01:08:17.05+00:00

    Please read the following Micorosoft document:

    Async Programming : Introduction to Async/Await on ASP.NET

    See Figure 2 A and Figure 3 in the above document.

    HttpResponseMessage response = client.GetAsync("Api/valuesGET").Result;

    will result in the situation shown in "Figure 2 A Two-Threaded Server Receiving Three Requests" - "the first two requests are assigned threads from the thread pool. Each of these requests calls an external resource, blocking their threads."

    HttpResponseMessage response2 = await client.GetAsync("Api/valuesGET");

    will result in the situation shown in "Figure 3 Waiting Asynchronously for an External Resource" - "the request thread has been returned to the thread pool while the asynchronous call is in progress."

    Therefore,

    "Now, if three requests were to come in, the server could cope easily. Because the threads are released to the thread pool whenever the request has asynchronous work it’s waiting for, they’re free to handle new requests, as well as existing ones. Asynchronous requests allow a smaller number of threads to handle a larger number of requests. Hence, the primary benefit of asynchronous code on ASP.NET is scalability."

    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2023-10-19T19:43:06.78+00:00

    the first blocks the thread until the request completes. the second frees the thread to do more work.

    with asp.net (especially core), the thread could be processing another request while waiting for the request. thus using .Result reduces scaling. (how fast and how many concurrent requests)

    note: using .Result with async calls may lead to deadlocked threads.

    so in general with asp.net MVC and asp.net core you should always use async.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.