Trying to use Redis Cache as a session store in an already existing Web App which is developed in asp.net mvc core ( 2.1.1) .
was referring https://garywoodfine.com/redis-inmemory-cache-asp-net-mvc-core/
and https://joonasw.net/view/redis-cache-session-store but when trying to check the session set/get values in Redis Desktop Manager nothing is shown.
Is there any additional steps required to make the session store to use the Redis Cache instead of the default in memory ( in-proc) option?
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedRedisCache(options =>
{
options.InstanceName = Configuration.GetValue<string>
("redis:name");
options.Configuration = Configuration.GetValue<string>
("redis:host");
});
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider();
services.AddSingleton<IDistributedCache, RedisCache>();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(60);
});
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Login}/{action=Login}/{id?}");
});
}
}
appsettings
"redis": {
"host": "127.0.0.1",
"port": 6379,
"name": "localhost"
},
Nuget package used
Microsoft.Extensions.Caching.Redis 2.1.1
Sample Usage in Action Method
var storedValue = "Redis TimeStamp : " + DateTime.Now.ToString("s");
HttpContext.Session.SetString("TestValue", storedValue);
HttpContext.Session.CommitAsync();
Appreciate any pointers or direction on this.
TIA