, but can i have any link of msdn which is specific to async for web api with aspnet-core.
It seems there might be some confusion regarding asynchronous calls in Web API and the async
/await
pattern in C#. Let's clarify a couple of points:
Web API and Asynchronicity
First, it's important to understand that Web APIs are inherently asynchronous from the caller's perspective. When multiple clients access a Web API simultaneously, they don't wait in a queue for their turn. The API handles these requests concurrently, giving the impression of simultaneous processing. This has always been the nature of web applications.
However, a critical point to add to this is: if your server-side code doesn't implement async
/await
throughout its asynchronous operations, you can indeed end up with clients waiting in a queue. Even though Web API itself is designed for concurrent handling, if your threads get blocked waiting for I/O-bound operations (like database calls or external service requests) to complete, the thread pool can become exhausted. When there are no more threads available to process new incoming requests, those requests will then be forced to wait, effectively creating a queue for your clients.
The async
/await
Pattern in C#
Second, the async
/await
keywords in C# are a powerful programming construct designed to manage thread resources efficiently when dealing with asynchronous operations. While Web APIs themselves are asynchronous, the async
/await
pattern in your C# code helps ensure that your server-side operations leverage this asynchronicity effectively.
Under the hood, when you use async
and await
, the C# compiler generates complex code to handle callbacks for asynchronous operations. This is where the concept of "asynchronous from start to end" comes into play. If a Web API action initiates an asynchronous call (e.g., to a database or an external service), it's crucial to use async
and await
throughout the entire call stack, including the Web API action itself.
Failing to adhere to this pattern can lead to wasted computing resources. Without async
/await
when performing an asynchronous operation, your main thread might be forced to wait idly for that operation to complete. This prevents the thread from being returned to the thread pool to process other incoming requests, even though the underlying Web API is still asynchronous. In essence, while the Web API remains asynchronous, your code can create bottlenecks and reduce efficiency by tying up threads unnecessarily.
https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/
https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/task-asynchronous-programming-model