Simon Libbs
Simon Libbs

Reputation: 103

Blazor Server Scoped Inititialized again on refreshing the page

I created a simple Blazor Server Application: The class "UserSettings.cs" is injected in the components.

     public class UserSettings
     {
         public Guid Id{ get; } = Guid.NewGuid();
     }
 
     services.AddScoped<UserSettings>();

I display the Id inside the UserSetting Service in different Components. After switching the component, the Id stays the same, but when i refresh the page, it changes. Why does the Service initialized again when refreshing the page?

Upvotes: 4

Views: 2179

Answers (1)

enet
enet

Reputation: 45754

Your service is scoped to the circuit connection...When you refresh the page, a new circuit object is created. You may use the AddSingleton instead, but in that case all your users may see the same ID.

Why not create the Id when your App is rendered for the first time, and save it in a session storage or local storage for use ?

Upvotes: 7

Related Questions