In my work a colleague made a statement, he said that it did not make sense to do an asynchronous method in c # if in ajax .
What would be the advantage of having an asynchronous call in
I understand that when referring to asynchrony in .net it refers to async/await
and the use of Task<>
The call that the client makes does not have to see how the call will be processed. Implementing asynchrony in the server will help the requests to be carried out in several thread and can be processed in parallel.
It is true that if you invoke the user's UI using an asynchronous ajax, it does not crash, but that is only a part. When the request arrives at the server, you should think about how it will scale the processing.
Introduction to async and await in ASP.NET
It is clear that if your server-side code is synchronous, creating a webapi will not make sense that it is asynchronous, the idea is that the access code to data, files, etc. is asynchronous so that the exposed service can scale in the server and answer a larger number of requests.
Why should I create async WebAPI operations instead of sync ones?
In summary, when you think of an asynchronous call from ajax you are thinking about how the UI responds to the user, when you think of asynchrony on the server side you are evaluating this scale to the multiple request
There is no relationship between one event and the other.
Beginning with the fact that practically every process inside the computer is asynchronous. Even when the code is synchronous, there is some background that is synchronizing it.
As for C #, there are several reasons to use async
and await
server side.
When you use async/await
, everything works in a ThreadPool
that synchronizes all the operations, without you having to put a lot of code on your part to achieve it.
So while a await
expects (for example) the database to end up doing its thing, the thread that started that Request is released and is available for another job. Then when the base ends, another Thread (maybe the same) takes over the task.
In short, async/await
brings performance and scalability to the web service.