今天来尝试一下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部分移除

dotnetcore5.0 session redis 使用 是否有阻塞

运行界面 如下, 表示项目okay

dotnetcore5.0 session redis 使用 是否有阻塞

 

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?}"); });
        }
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2021-09-05
  • 2021-07-22
  • 2021-09-24
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2021-09-30
猜你喜欢
  • 2021-06-07
  • 2021-12-24
  • 2021-12-12
  • 2021-11-28
  • 2021-12-21
  • 2021-06-12
  • 2021-08-09
相关资源
相似解决方案