Preface
This articleArticleMainly for the coming ASP. the asynchronous controller in net mvc4 makes a Review and Outlook, and does not discuss the use of asynchronous controllers. If you are still confused about when to use Asynchronous controllers, this may disappoint you, however, there are a lot of discussions and analyses in this area. You can also search for them by yourself. okay, let's talk about it.
Asynchronous Controller
In the era that Ms is increasingly advocating asynchronous operations, Asp. net MVC asynchronous operations have been relatively outdated. for developers, implementing an asynchronous controller usually takes more time than a common controller.Code. This feature is implemented in ASP. net MVC 2 has been introduced, and it has not been changed. Until now, with the coming of C #5 and async/await, the asynchronous controller is as concise as the common controller operation code. now (of course, you have to wait for ASP. net mvc4 ,. net 4.5 and C #5 after the official release), you can write the following concise code:
PublicAsync task <viewresult> foobar ()
{
ReturnView (await dosomething ("Some actions"));
}
How? It's really easy ~~
The outlook is important, but before that, let's review the implementation of the Controller in ASP. net mvc 2 to 4. The comparison is often more impressive.
Note: The following examples of ASP. net mvc 4 are based on ASP. net mvc Developer Preview. In the official version, this implementation may change.
Asynchronous controller in ASP. net mvc 2/3
In ASP. net mvc 2/3, to implement a one-step controller, You have to implement two methods: xxxasync and xxxcompleted. At the same time, your controller must be changed to inherit fromAsynccontroller,There are already many examples of this implementation and explanation. Here I will directly move an example from msdn.
To look at asynchronous controllers, Let's first look at the synchronous Implementation of the same function. Everyone should be familiar with it:
Public ClassPortalcontroller: Controller
{
PublicActionresult News (StringCity)
{
Newsservice =NewNewsservice ();
Viewstringmodel headlines = newsservice. getheadlines (city );
ReturnView (headlines );
}
}
Let's look at its asynchronous implementation method:
Public Class Portalcontroller: asynccontroller
{
Public Void Newsasync ( String City)
{
Asyncmanager. outstandingoperations. increment ();
VaR Newsservice = New Newsservice ();
Newsservice. getheadlinescompleted + = (sender, e) =>
{
Asyncmanager. Parameters [ " Headlines " ] = E. value;
Asyncmanager. outstandingoperations. decrement ();
};
Newsservice. getheadlinesasync (city );
}
Public Actionresult newscompleted (String [] Headlines)
{
Return View ( " News " , New Viewstringmodel
{
Newsheadlines = headlines
});
}
}
Looking at the above implementation, I have to admit that compared with the synchronous controller, the asynchronous action developers still need to do more work. however. in the big environment of net4.0, we can only use this method.
Of course, the msdn example is a standard hierarchical implementation example. In this example, you will have to implement your own service layer. If you just want to simply call asynchronous action, is there a convenient way? The answer is yes. in net 4.0, Microsoft introduced the task class, and interested students can slam here. with task, if you just want to perform asynchronous operations in a simple function, you can also drop:
Public Class Portalcontroller: asynccontroller
{
Public Void Newsasync ( String City)
{
Asyncmanager. outstandingoperations. increment ();
VaR Task = task. Factory. startnew () => runthread (city ));
Task. continuewith (t =>
{
Asyncmanager. Parameters [ " Headlines " ] = T. result;
Asyncmanager. outstandingoperations. decrement ();
});
}
Public Actionresult newscompleted ( String [] Headlines)
{
Return View ( " News " , New Viewstringmodel
{
Newsheadlines = headlines
});
}
Private String Runthread ( String Input)
{
Thread. Sleep ( 5000 );
Return Input;
}
}
Of course, the Code is not much reduced, but it is not very complicated. Although it is not so nice, it is not too ugly.
Next let's take a look at the asynchronous controller in ASP. net mvc 4.
Asynchronous controller in ASP. NET 4 Developer Preview
Abandon the pair. net 3, Asp. net MVC 4 fully embraces the task class library. You do not need to write two methods for each action, or manually increase or decrease the asynchronous action counter (Asyncmanager. outstandingoperations. increment ()Now, you just need to pick up your finger and tap a few times. Other things will be done by the system for you.
Public ClassPortalcontroller: asynccontroller
{
PublicTask <viewresult> News (StringCity)
{
ReturnTask. Factory. startnew () => runthread (city ))
. Continuewith (t =>
{
ReturnView (NewViewstringmodel ()
{
TEXT = T. Result
});
});
}
Private StringRunthread (StringInput)
{
Thread. Sleep (5000);
ReturnInput;
}
}
Is it much better? Lamda makes everything better:
PublicTask <viewresult> News (StringCity ){
ReturnTask. Factory. startnew () => runthread (city ))
. Continuewith (t => View (NewViewstringmodel {text = T. Result }));
}
So, are we going to say goodbye here? No. Please continue.
Great async/await
Although it is not yet officially released, if we follow Microsoft's eyes to go further, in ASP.. NET and C #5. net 4 added the async function. With the async and await keywords, asynchronous encoding is simpler. This also includes operations related to asynchronous controllers:
Public ClassPortalcontroller: asynccontroller {
PublicAsync task <viewresult> News (StringCity)
{
ReturnView (NewViewstringmodel ()
{
TEXT = await newthread (city)
});
}
PrivateAsync task <String> Newthread (StringInput)
{
Thread. Sleep (5000);
ReturnInput;
}
}
Summary
with the help of async, await keywords, and task class libraries, we can operate asynchronous controllers like ordinary controllers in the foreseeable future, but like many other new ones. the same as net features, the larger the capability, the greater the responsibility, and convenience often mean abuse. asynchronous controllers are good, but they are not suitable for every scenario. Improper use of them often leads to server switching between different threads, this also brings more additional overhead. in the development field, we should pay special attention to the performance, which is often more important than anything else. Therefore, please use Asynchronous controllers when the performance is indeed high and the user can respond accordingly.