I am trying to use ISession
in a .net Blazor web app server side only. My problem is that the ISession in very unstable as it disappear and comeback when it feels like it.
First in the program.cs i added the session like so :
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30); // Set timeout
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
builder.Services.AddHttpContextAccessor();
And right after the build :
var app = builder.Build();
I added the session to it
app.UseSession();
Now here the default home page that comes in an empty project and i added a button to demonstrate with console write to see what is happening :
@page "/"
@page "/Home"
@inject IHttpContextAccessor hca
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<button @onclick="Click">click</button>
@code{
protected override void OnInitialized()
{
Console.WriteLine($@"Home.Initialized - SessionId = {hca.HttpContext.Session.Id}");
Console.WriteLine($@"Home.Initialized - Keys.Count = {hca.HttpContext.Session.Keys.ToList().Count}");
hca.HttpContext.Session.SetString("test", "tesatassad");
}
private void Click()
{
hca.HttpContext.Session.SetString("test1", "tesatassad");
hca.HttpContext.Session.SetString("test2", "tesatassad");
Console.WriteLine($@"Home.Click - SessionId = {hca.HttpContext.Session.Id}");
Console.WriteLine($@"Home.Click - Keys.Count = {hca.HttpContext.Session.Keys.ToList().Count}");
}
}
The whole idea is to log the session id and the amount of keys in it on initialized and when i click the button. Now just running the page i have the first issue. The pages always trigger OnInitialized
twice.
I made the log purposely before the new key is created to see it so the log shows that the second trigger is valid (which make sense)
Home.Initialized - SessionId = 3c3f9a90-7273-f76f-0368-843e3f01f374
Home.Initialized - Keys.Count = 0
Home.Initialized - SessionId = 3c3f9a90-7273-f76f-0368-843e3f01f374
Home.Initialized - Keys.Count = 1
Now lets click the button. The button set 2 new key and display the count after and check the full log again
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 0
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 1
Home.Click - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Click - Keys.Count = 3
Now this make sense, i have my double load and i have the log after the set in the button showing addition of 2 keys for a total of 3.
Now let’s refresh the page with F5 and check the log :
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 0
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 1
Home.Click - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Click - Keys.Count = 3
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 1
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 3
Again you see the double log but the first is wrong. It lost all the key but the session id is still the same. What is this coming from ? how can you tell when to try to read session only on the second OnInitialized
?
Now this is even weirder, Let’s click F5 again to refresh the page once more
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 0
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 1
Home.Click - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Click - Keys.Count = 3
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 1
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 3
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 3
Home.Initialized - SessionId = 846bc865-7b0f-de46-c4d0-f015eb1b4389
Home.Initialized - Keys.Count = 3
Now the second F5 the session keys exist in both call of OnInitialized
. From now on it will work for any navigation combination you want and the session is always up to date.
My major problem is that i need to load data in the page in the OnInitialized
but it need to use values stored server side in the ISession
. Being so unstable there must be something i am missing