locked
Blazor Serverside Singleton becomes specific to each user RRS feed

  • Question

  • User-1771888428 posted

    Currently in blazor server, Singleton is good, that is with page refresh it won't reset, But a big problem of it is not specific to each user and is shared between all users  in the app.

    I created a web site thinking my shopping cart as singleton is good, But when I deployed it, All users could see each other shopping cart and when one user is adding an item, All other users see it.

    I resorted to scoped service that was specific to each user, but then refreshing the page reset the shopping cart.

    I think if singleton services in blazor serverside become specific to each user, Many problems of Blazor server including authentication will solve.

    Is Blazor team planning to create a type of service that has these features meaning, It is specific to each user and doesn't reset by page refresh?

    Friday, March 27, 2020 1:51 PM

All replies

  • User475983607 posted

    Currently in blazor server, Singleton is good, that is with page refresh it won't reset, But a big problem of it is not specific to each user and is shared between all users  in the app.

    Correct.  A singleton is global to the entire web application and every application user.  You'll need to use rethink the design and use standard state management APIs to identify the user like Identity, cookies, session, and SQL tables.

    You can also check out the official docs for assistance.

    https://docs.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.1&tabs=visual-studio

    Friday, March 27, 2020 2:21 PM
  • User-1771888428 posted

    The thing that is missing in Blazor server side is Session (a facility that is specific to each user and won't reset by refresh), I am looking for something that works as a good session, Current implementations of sessions in Blazor server can not be used in initialized and parametersset events and can only be used in onafterrender, that limits their usefulness.  I wish session will be available in the next version.

    Saturday, March 28, 2020 9:13 AM