Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi, I am newbie in mvc.
I have seen few articles on asynchronous views and few on asynchronous controllers.
Usage of which ones gives the best performance. what are the pros and cons of each?
Posted

1 solution

Task Parallel Library(TPL) that has been introduced in MVC 4 allow you to handle concurrent requests within a pool. ASP.Net without TPL is limited in the number of requests it can handle concurrently by the number of threads in a thread pool. ASP.Net that uses the TPL is limited by the CPU/memory/IO of the machine handling requests.

The TPL does not consume threads in the way you seem to think it does. Tasks are not threads, they are a wrapper around some unit of computation. The task scheduler is responsible for executing each task on any thread that is not busy. At runtime, awaiting a task does not block the thread, it merely parks the execution state so that it may proceed at a later time.

In normal case, a single HTTP request would be handled by a single thread, completely removing that thread from the pool until a response is returned. Having the TPL, you are not bound by this constraint. Any request that come in starts a continuation with each unit of computation required to calculate a response able to execute on any thread in the pool. With this model, you can handle many more concurrent requests than with standard ASP.Net.


The AsyncController class enables you to write asynchronous action methods. You can use asynchronous action methods for long-running, non-CPU bound requests. This avoids blocking the Web server from performing work while the request is being processed.


The async actions are useful only when you are performing I/O bound operations such as remote server calls. The benefit of the async call is that during the I/O operation, no ASP.NET worker thread is being used. Consider the following for both sync and async operation for a login action:

a. When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.
b. The IdentityManager.Authentication.CheckPasswordAndSignIn method is invoked. This is a blocking call and during the entire call the worker thread is being jeopardized.

And here's how the 2nd request works:

When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.

a. The IdentityManager.Authentication.CheckPasswordAndSignInAsync is called which returns immediately. An I/O Completion Port is registered and the ASP.NET worker thread is released to the thread pool.
b. Later when the operation completes, the I/O Completion port is signaled, another thread is drawn from the thread pool to finish returning the view.

As you can observe, the 2nd case ASP.NET worker threads are used only for short period of time which means there are more threads available in the pool for serving other requests.

By the way, asynchronous views - I haven't heard about these?

You can follow a simple example at : Asynchronous Programming in Web API /ASP.NET MVC[^]

Hope this will help.

Mostafa
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900