I am trying to use distributed SQL Server Cache for session state for a ASP.NET 6 application. The code example on Microsoft documentation shows how to set up session state using in memory cache: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-6.0
But our application will be deployed to multiple servers. So I am looking for an approach to use distributed SQL Server Cache for session state.
Here is the code I am using in Program.cs:
builder.Services.AddDistributedSqlServerCache(options =>{
options.ConnectionString = builder.Configuration.GetConnectionString(
"DistCache_ConnectionString");
options.SchemaName = "dbo";
options.TableName = "TestCache";}); // we have the corresponding table "TestCache" set up
builder.Services.AddSession(options =>{
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.IsEssential = true;});
...
app.UseSession();
When trying to set a session value in a controller, id is empty and a is null. And the "TestCache" table is also empty.
HttpContext.Session.SetString("name", "test");
var id = HttpContext.Session.Id;
var a = HttpContext.Session.GetString("name");
Did I miss anything for configuration? I searched online and found some examples using similar code, but all of them seem to use .NET Core 2.* or .NET Core 3.* Did anything change for .NET 6?