First of all, a small ASP. NET MVC 4 sample, the code is as follows:
HomeController:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.SessionState;namespacemvcapplication2.controllers{ Public classHomecontroller:controller {// //GET:/home/ PublicActionResult Index () {returnView (); } PublicActionResult Test () {//In order to test execution of the code waiting for 30 seconds, the actual project will encounter//a lot of things that take a long time, like calling WebService, or dealing with big dataSystem.Threading.Thread.Sleep (30000); returnView (); } }}
Global.asax:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;namespacemvcapplication2{//note:for instructions on enabling IIS6 or IIS7 Classic mode,//Visithttp://go.microsoft.com/?LinkId=9394801 Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (Bundletable.bundles); } protected voidSession_Start (Objectsender, EventArgs e) { } }}
The code in views is omitted.
Such a simple program if we run will find a problem: When I open the browser Open two tab, first in a tab to open/home/test, then this tab will wait 30 seconds to render the page, during which I opened/home/index in another tab, The second tab will render/home/index only after the first tab30 seconds have elapsed. And this situation if the actual project will greatly affect performance, such as if we need to access webservice spend a lot of time to get some data, and we have to send a request through AJAX to process a query, if you follow the above practice, You can only wait for the WebService method to finish executing the AJAX request. (Note: This situation only exists under the same session, if the above operation does not open two tab instead of two browser, similar situation will not occur)
Solution:
Here we need to look at the role of sessionstate in ASP. NET MVC:
A piece of article in StackOverflow Records to:
Session State was designed so, only one request from a particular user/session occurs at a time. So if you had a page that had multiple AJAX callbacks happening at once they would be processed in serial fashion on the s Erver. Going session-less means that they would execute in parallel.
The main idea is that the session state is used to process requests in the same session sequentially, and if parallel processing is required, set session-less.
How do I set up sessionstate?
In Mvc4, you can add the following attribute to the controller: (Under the System.Web.SessionState namespace)
[SessionState (Sessionstatebehavior.readonly)]
MVC3 Add the following attribute:
[Controllersessionstate (Sessionstatebehavior.readonly)]
Sessionstatebehavior the difference between the four options:
- Default –same as Required.
- Required –a full Session lock is taken, and requests to this controller are serialized. the normal MVC behavior.
- ReadOnly –a Session Lock is Not taken, and a controller can read values in Session, but it cannot modify the values in session. requests to this Controller would be parallelized.
- Disabled –session is Unavailable within this controller, and attempts to use it would be met with a error. requests to this controller WI ll be parallelized