Asp.net mvc 中處理同一個session的並行請求的問題

首先來一個小的asp.net mvc 4的sample,代碼以下:web

HomeController:ajax

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中的代碼省略。session

這麼簡單的程序若是咱們運行會發現一個問題:當我打開瀏覽器打開兩個tab,先在一個tab中打開/Home/Test,這時這個tab會等30秒才能呈現頁面,在這期間我在另外一個tab中打開 /Home/Index,只有等第一個tab30秒事後,第二個tab才能呈現/Home/Index。而這種狀況若是在實際項目中會很是影響性能,好比咱們若是須要訪問webservice耗用大量時間獲取一些數據,同時咱們又要經過ajax發送請求去處理一個查詢,這時若是按照上面的作法,只能等待webservice方法執行完畢後才能處理ajax請求。(Note:這種狀況只有在同一個session下存在,若是上面的操做不打開兩個tab而是換成兩個瀏覽器,相似狀況不會發生)mvc

 解決方案:app

這裏咱們須要瞭解一下asp.net mvc 中的SessionState的做用:asp.net

在stackoverflow的一片文章記錄到:less

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
相關文章
相關標籤/搜索