今天来尝试一下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?}"); }); } } }