3
Answers

how or where async function in a web api controller can help me?

Photo of rajesh yadav

rajesh yadav

1w
168
1

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

Answers (3)

0
Photo of Muhammad Imran Ansari
212 8.9k 343.5k 1w

Use async in Web API when performing I/O operations. it improves performance, scalability, and responsiveness under load.

Q-1 Ans:

if your controller interacts with databases (like EF Core), external APIs, or file systems. Even though JavaScript can call your API asynchronously, using async on the server frees up threads during I/O operations, allowing more requests to be handled efficiently.

Q-2 Ans:

  • High-traffic Web APIs: Handles more users without blocking threads.
  • Long-running I/O tasks: like DB queries, API calls, or file access — improves responsiveness.
  • Cloud/hosted apps (e.g., Azure App Service): Scales better under load with fewer resources.
  • Apps with many concurrent operations: async prevents thread pool exhaustion and improves performance.

Good Luck!

Accepted
0
Photo of rajesh yadav
1.5k 100 12k 1w
[HttpGet]

    public async Task<ActionResult<IEnumerable<Dsp>>> GetDsp()

    {

        return await _context.Dsp.ToListAsync();

    }

 

thank u , is there any central configuration for a solution or project?

or i have to use as i have pased in above code. 

0
Photo of rajesh yadav
1.5k 100 12k 1w

in my case i have called a method of controller from javacript and method returns one table. so kindly explain this senario how i am getting benefited by async method raher than sync method of the controller ?