今天來嘗試一下dotnetcore5.0 的session redis的用法, 順便看看你是否有阻塞,在我以前的文章 你的項目真的需要Session嗎?redis保存session性能怎么樣? 和 asp.net mvc Session RedisSessionStateProvider鎖的實現 有提到asp.net mvc session 鎖的問題【默認內存保存是不存在】,前幾天 發現beego session redis 是沒有鎖的 ,大家可以參考beego Session redis存儲以及是否阻塞 和 beego Session redis源碼解讀, 這次我打算用VScode來創建項目,VScode的搭建可以參考 使用Visual Studio Code開發.NET Core看這篇就夠了
1.創建項目SessionDemo,把launchSettings.json 的https部分移除
運行界面 如下, 表示項目okay
2添加必要的包
dotnet add package Microsoft.AspNetCore.DataProtection.StackExchangeRedis dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis dotnet add package Microsoft.AspNetCore.Http //使用Session時有擴展方法 dotnet add package Microsoft.AspNetCore.Session
修改Startup.cs文件如下

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.AspNetCore.DataProtection; using StackExchange.Redis; namespace SessionDemo { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); // add by gavin for session redis var redisConn = "127.0.0.1:6379"; services.AddDataProtection() .SetApplicationName("vextus") .PersistKeysToStackExchangeRedis(ConnectionMultiplexer.Connect(redisConn),"DataProtection-Keys"); services.AddStackExchangeRedisCache(o => { o.Configuration = redisConn; }); services.AddSession(); services.AddMvc(x=> { x.EnableEndpointRouting = false; }); //寫controller 路由需要 } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); // add by gavin for session redis app.UseSession(); app.UseMvc(x => { x.MapRoute( name: "default",template: "{controller=Home}/{action=Index}/{id?}"); }); } } }
創建一個HomeController.cs 如下:

using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SessionDemo { public class HomeController : Controller { public IActionResult Index() { this.HttpContext.Session.SetString("name", "test"); return Content("Home"); } public IActionResult V1() { var txt = this.HttpContext.Session.GetString("name"); Thread.Sleep(1000 * 4); this.HttpContext.Session.SetString("name", "V1"); return Content("Get Session:"+txt+" set session:V1"); } public IActionResult V2() { var txt = this.HttpContext.Session.GetString("name"); Thread.Sleep(1000 * 1); this.HttpContext.Session.SetString("name", "V2"); return Content("Get Session:" + txt + " set session:V2"); } public IActionResult V3() { var txt = this.HttpContext.Session.GetString("name"); return Content("Get Session:" + txt); } } }
修改Index.cshtml 如下:

@page @model IndexModel @{ ViewData["Title"] = "Home page"; } <div class="text-center"> <div>test</div> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <input type="button" value="set session" id="setsession" /><br /> V1: <p id="txt1"></p><br /> V2: <p id="txt2"></p><br /> <input type="button" value="get session" id="getsession" /><br /> V3: <p id="txt3"></p><br /> <script> $("#setsession").click(function(){ $.get("http://localhost:5000/home/v1",function(data){$("#txt1").html(data) ;}); $.get("http://localhost:5000/home/v2",function(data){$("#txt2").html(data) ;}); }); $("#getsession").click(function(){ $.get("http://localhost:5000/home/v3",function(data){$("#txt3").html(data) ;}); }); </script> </div>
運行項目, 首先訪問http://localhost:5000/home/index 設置session, 然后在進入http://localhost:5000/ ,頁面首先點擊 setsession, 完全返回后 ,再次點擊get session。
結論setsession同時發起V1和V2的請求,V1和V2獲取的session 是先前index 設置的test,V2 把session設置為V2保存,由於V1等待時間長一些, 所以V1在V2后面設置session值V1, 后面V3 獲取的session是V1。同時V1和V2的響應時間就說明V1和V2之間沒有阻塞。看看redis里面的數據