0

I've been going through the aspnet core 3 Rest API tutorial here (Code below)...

https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio

I want to host this in a console app. My understanding is that this uses "Kestrel" as a web server. Lets assume a request takes 10 seconds.

If two requests came in at the same time, what exactly would be different between two scenarios where one used Async Await Task , and one did not ( returned normal ActionResult) in my Console App?

[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
    var todoItem = await _context.TodoItems.FindAsync(id);

    if (todoItem == null)
    {
        return NotFound();
    }

    return todoItem;
}
fangled
  • 21
  • 4

1 Answers1

0

If two requests came in at the same time, what exactly would be different between two scenarios where one used Async Await Task , and one did not ( returned normal ActionResult) in my Console App?

In my opinion,what you mean is the differences between async and sync method in asp.net core web api.

Synchronous means two or more operations are running in a same context (thread) so that one may block another.

If an API call is synchronous, it means that code execution will block (or wait) for the API call to return before continuing. This means that until a response is returned by the API, your application will not execute any further, which happens sequentially.

Making an API call synchronously can be beneficial, however, if there if code in your app that will only execute properly once the API response is received.

Asynchronous means two or more operations are running in different contexts (thread) so that they can run concurrently and do not block each other.

For more details,refer to

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

Not much difference between ASP.NET Core sync and async controller actions

WebApi async vs sync

What's a sync and async method?

Ryan
  • 19,118
  • 10
  • 37
  • 53
  • Thanks very much for this. Some of the links you have provided are very interesting, but it doesn't quite answer the question. I put in a 10 second delay in my "not async" service, but the service did not block. So, perhaps my question is why ( especially when hosted in a console app) , and how does this compare to using async/await ( again, in a console set up) – fangled Dec 09 '19 at 14:29