首先来一个小的asp.net mvc 4的sample,代码如下:

HomeController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
namespace MvcApplication2.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Test()
        {
            //这里为了测试执行了等待30秒的代码,实际项目中会遇到
            //很多类似需要很长时间的情况,比如调用webservice,或者处理很大数据
            System.Threading.Thread.Sleep(30000);
            return View();
        }
    }
}

 

 Global.asax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcApplication2
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
        protected void Session_Start(object sender, EventArgs e)
        {
        }
    }
}

 

 

Views中的代码省略。

这么简单的程序如果我们运行会发现一个问题:当我打开浏览器打开两个tab,先在一个tab中打开/Home/Test,这时这个tab会等30秒才能呈现页面,在这期间我在另一个tab中打开 /Home/Index,只有等第一个tab30秒过后,第二个tab才能呈现/Home/Index。而这种情况如果在实际项目中会非常影响性能,比如我们如果需要访问webservice耗用大量时间获取一些数据,同时我们又要通过ajax发送请求去处理一个查询,这时如果按照上面的做法,只能等待webservice方法执行完毕后才能处理ajax请求。(Note:这种情况只有在同一个session下存在,如果上面的操作不打开两个tab而是换成两个浏览器,类似情况不会发生)

 解决方案:

这里我们需要了解一下asp.net mvc 中的SessionState的作用:

在stackoverflow的一片文章记录到:

Session state is designed so that only one request from a particular user/session occurs at a time. So if you have a page that has multiple AJAX callbacks happening at once they will be processed in serial fashion on the server. Going session-less means that they would execute in parallel.

大意就是Session state是用来将同一个session的请求按顺序处理,如果需要并行处理则需要设置session-less。

 

如何设置SessionState呢?

mvc4中可以在controller上面加如下Attribute:(在System.Web.SessionState命名空间下)

[SessionState(SessionStateBehavior.ReadOnly)]

 

 mvc3加如下Attribute:

[ControllerSessionState(SessionStateBehavior.ReadOnly)]

 

 SessionStateBehavior四个选项中的区别:

  • Default – Same as Required.
  • Required – A full Session lock is taken, and requests to this controller are serialized.  This is 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 will be parallelized.
  • Disabled – Session is unavailable within this controller, and attempts to use it will be met with an error.  Requests to this controller will be parallelized