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

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   

首先來一個小的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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.