ISession instabilities

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật