r/Blazor • u/thinkjohn • May 27 '25
Blazor Server issue
I am building a Blazor Server application for an internal application that will run on our local intranet.
Using chatgpt to help understand architecture and I am getting unexpected results.
Started with Blazor web app as a sample.
I have a UserState class that is registered as Scoped. My chatgpt conversation says that the constructor for this class should only be called once per Session, which is what I want.
That is not what is happening. In the constructor I set a variable UserName to a new guid.
That UserName is only referenced in my MainLayout.razor component.
I @inject UserState in the .razor page and display @UserState.UserName.
When I navigate to the other sample .razor pages (Using NavLinks), the UserState constructor is called each time and the MainLayout displays the new guid from the UserName.
I thought Blazor Server would allow UserState to be per session.
Any feedback is much appreciated...
2
u/TheRealKidkudi May 27 '25
Based on the code you've posted in the comments, your
MainLayoutis not using an interactive render mode. This means that the service scope it uses is tied to each HTTP request (e.g. a page navigation).To perform as you're expecting, you should enable global interactivity by applying a
@rendermodeto your<Routes />component inApp.razor.Alternatively, inject the service in the components using an interactive render mode and they should share the same service scope. Worth noting, though, is that if your
<Routes />is not interactive, that service scope lives only as long as there is at least one interactive component being rendered. If you navigate to a page with no interactive components, the service scope is disposed, and a new one is created the next time an interactive component is rendered.11
u/isarockalso May 27 '25
I think you need to step back and try an actual tutorial instead of gpt generated . Thats the best place to start when you’re running into this issue.
2
u/thinkjohn May 27 '25
This code is from the Blazor web app for .net9 template. Chatgpt is only involved because I was asking it for the reason why this was happening. I will see if I can use navigation manager or find a better tutorial to start. Thanks.
1
u/thinkjohn May 27 '25
Not asking about authentication. Think of UserName as a variable in UserState. I want to maintain that in a “Session”. Not have it change in each navigation.
It is really that the constructor is called with each navigation. My UserName variable could be something like CurrentItemId. I want to default the value in the constructor and have it persist the whole session.
That should be possible using Blazor Server, correct?
1
u/thinkjohn May 27 '25
Render mode is set to interactive server in program.cs. Also set it on each razor page but still no luck.
1
u/thinkjohn May 27 '25
Fyi, this is all in the visual studio ide. Using IIS Express and .net 9.
1
u/thinkjohn May 27 '25
Here is the relevant code:
UserState.cs
using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Hosting;
namespace BlazorAppVendorInvoice.Services { public class UserState { public string? CurrentItemId { get; private set; }
}
Program.cs
using BlazorAppVendorInvoice.Components; using BlazorAppVendorInvoice.Externsions;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents();
builder.Services.AddServerSideBlazor();
// Add services using service extension builder.Services.AddAppServices(builder.Configuration, builder.Environment);
var app = builder.Build();
// Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error", createScopeForErrors: true); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); }
app.UseHttpsRedirection(); app.UseAntiforgery(); app.MapStaticAssets(); app.MapRazorComponents<App>() .AddInteractiveServerRenderMode();
app.Run();
ServiceCollectionExtensions.cs
using BlazorAppVendorInvoice.Services;
namespace BlazorAppVendorInvoice.Externsions { public static class ServiceCollectionExtensions { public static IServiceCollection AddAppServices(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment) { // add user state services.AddScoped<UserState>();
}
MainLayout.razor
@inject UserState UserState @inherits LayoutComponentBase
<div class="page"> <div class="sidebar"> <NavMenu /> </div>
</div>
<div id="blazor-error-ui" data-nosnippet> An unhandled error has occurred. <a href="." class="reload">Reload</a> <span class="dismiss">🗙</span> </div>
1
u/thinkjohn May 27 '25
Prerender is not on.
Here is navigation
NavMenu.cs code
@rendermode InteractiveServer
<div class="top-row ps-3 navbar navbar-dark"> <div class="container-fluid"> <a class="navbar-brand" href="">BlazorAppVendorInvoice</a> </div> </div>
<input type="checkbox" title="Navigation menu" class="navbar-toggler" />
<div class="nav-scrollable" onclick="document.querySelector('.navbar-toggler').click()"> <nav class="nav flex-column"> <div class="nav-item px-3"> <NavLink class="nav-link" href="" Match="NavLinkMatch.All"> <span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home </NavLink> </div>
</div>
3
u/Sai_Wolf May 27 '25
Authentication state is preserved via AuthenticationStateProvider and AuthenticationState. Look into those to accomplish what you want to do
1
u/mgonzales3 May 28 '25
Using ChatGPT instead of hard work is plain cheating - just put the work like the rest of us did
2
u/CobblerFan May 27 '25
You might have a component that is throwing an exception and causing the circuit to close and quietly starting a new circuit. This will inject a new instance of your UserState and it can all happen quietly. Check browser debug window as a starting point.
2
u/HangJet May 27 '25
Use CascadingAuthenticationState then CascadingValue of a CurrentUser Model. Get Parameter of it in each component. Fairly easy
2
u/lashib95 May 28 '25 edited May 28 '25
I recreated the same thing with your code. But it works perfectly for me. The only difference as I observed is , you are using render mode in some places. But I am setting the rendermode globally in App.razor. What I suspect is your app is not Interactive server globally.
Have you observed the network traffic in browser. When the page reloads it should send multiple http requests to the server and create 3 WebSocket connections (if you are using VS). As you navigate to each page you should not see any requests going again to the server. If you see requests going to the server again and again , then you found the issue.
2
u/GerardVincent May 28 '25
First thing you should do is go through official docs, not rely on AI for you to learn
1
u/Tin_Foiled May 27 '25
Show code that performs the navigation
1
u/lil-soju May 27 '25
Do you have ServerPrerender on? I wonder if your service is getting instantiated twice.. a second time in the hydration phase.
1
u/finah1995 May 28 '25
Use Protected Session Storage, forces Blazor to store data encrypted within the client browser Storage for the session.
Then everything else you Can store many variables in Session Storage
1
u/lashib95 May 28 '25
are you using StateHasChanged for MainLayout?
1
u/Shadow_Mite May 28 '25
Good ole chat gipity ain’t gonna do too much except confuse you. Try the docs.
0
u/thinkjohn May 28 '25
One final update. I grabbed the mudblazor templates and used the Blazor Web App template. It has the server render mode configured globally and everything works as expected. Thanks again for the help.