how or where a sync function in a web API controller can help me?

rajesh yadav 171 Reputation points
2025-06-24T13:12:08.41+00:00

hi,

I just made one web API with core 9 with entity framework and all actions, i found that all the methods are made with a sync.

q1) so my question is where or how it can help me , because in my case i call my controllers action methods from java script in JavaScript i can can it in async or synchronous way, question came to my mind do i need async in the action methods of controller.

q2) and if i am not beneficial in above case then where i would be?

yours sincerely

Developer technologies ASP.NET ASP.NET API
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2025-06-24T14:29:35.2766667+00:00

    In computing, an asynchronous operation runs separately from the main program. This lets your program keep working on other tasks instead of waiting for that operation to finish.

    For example, C# uses the async/await pattern to handle server-side logic asynchronously. You can see a great explanation of this here: https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/

    Similarly, AJAX calls from JavaScript in a web browser are asynchronous by default. The JavaScript application making the call doesn't know (or need to know) if the server endpoint it's talking to is asynchronous or synchronous.

    Given this, what specific problem are you trying to solve?

    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2025-06-24T15:51:00.7833333+00:00

    the javascript calls are asynchronous as network calls are asynchronous by nature. to javascript it doesn't matter if the webapi method is an async method or not, its still async to javascript.

    asp.net core applications should use async operations when available because this improves scaling performance. asp.net core runs a fixed number of processing pipeline threads, so long running sync operation can block request processing.

    database calls are slow in request processing terms. so for the best scaling and performance async database calls should be used. if your website will not have a heavy load (concurrent users), then it may not matter.

    0 comments No comments

  3. SurferOnWww 4,631 Reputation points
    2025-06-25T01:34:03.3533333+00:00

    q1) so my question is where or how it can help me , because in my case i call my controllers action methods from java script in JavaScript i can can it in async or synchronous way, question came to my mind do i need async in the action methods of controller.

    First, please keep in mind that client side (regardless of JavaScript) does not matter as far as you are talking about async/await on ASP.NET.

    The answers to your q1) are:

    (1) Yes, as long as you use async/await for I/O bound tasks such as reading and writing files, database records, and REST APIs.

    (2) No, if you use async/await for CPU-bound tasks.

    Please read the following Microsoft document for detail:

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

    q2) and if i am not beneficial in above case then where i would be?

    Do not use async/await for CPU-bound tasks. If you have CPU-bound tasks on ASP.NET, your best bet is to just execute it directly on the request thread.


  4. Jack Dang (WICLOUD CORPORATION) 225 Reputation points Microsoft External Staff
    2025-06-26T02:19:31.5433333+00:00

    Hi @rajesh yadav ,

     

    Thanks for your thoughtful questions about using synchronous vs. asynchronous methods in your ASP.NET Core 9 Web API project with Entity Framework. Let’s break down your concerns and clarify when and why async might be beneficial.

     

    Q1: You asked if you call your controller actions from JavaScript (which can be async or sync), do you need to make your controller methods async?

    Great question! The short answer is: yes, you should consider making your controller methods async—especially when they involve I/O-bound operations like database access.

    Here’s why:

    • Even if your JavaScript code is asynchronous, the server-side method still runs on a thread from the ASP.NET Core thread pool.
    • If your controller method is synchronous and performs I/O (like querying a database), it blocks that thread until the operation completes.
    • In contrast, an async method releases the thread while waiting for the I/O operation to finish, allowing the server to handle other requests in the meantime.

    So, while your JavaScript can be async, the real benefit of async on the server side is better scalability and responsiveness under load—not just matching the client’s behavior.

     

    Q2: You also asked if you don’t see a benefit in your current case, when would async be useful?

    You might not notice a difference in small or low-traffic applications, but async becomes valuable in these scenarios:

    • High-concurrency environments: If your API is expected to handle many simultaneous requests, async helps prevent thread starvation.
    • I/O-heavy operations: When your controller interacts with databases, file systems, or external APIs, async avoids blocking threads unnecessarily.
    • Cloud-native or microservices architectures: Async patterns are more efficient in distributed systems where latency and throughput matter.

    In short, async is about resource efficiency. It allows your app to do more with fewer threads, which is especially important in production environments or when scaling.

    If you want to implement it to your project, kindly visit this link for reference:
    https://procodeguide.com/programming/async-web-api-with-aspnet-core/

    Hope my answer would help. If you have extra questions, please click "Comment".


  5. AgaveJoe 30,126 Reputation points
    2025-06-28T10:24:36.3433333+00:00

    , 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

    0 comments No comments

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.