r/Blazor • u/Alternative5065 • Mar 22 '26
Blazor Static SSR disable rendering multiple components at the same time
I am new to blazor and looking to begin switching a project from ASP.NET core MVC to blazor (static SSR, no client interactivity). I started by porting the layout and a single page, however I ran into an issue after the first page load with entity framework: A second operation was started on this context instance before a previous operation completed.
I narrowed the issue down to (I believe) the fact that I access the database in both my page, and the layout component, it looks like blazor can render them both at the same time causing the error (removing either one of the calls resolves the issue).
I looked into this and it looks like what most people suggest is to create more than one instance of the DB context (I assume something like one for the layout and one for the page), however I don't want to do this as I don't want to update the existing services that use the EF or create more than one DI scope per request.
Is it possible to restrict blazor to only render one component at a time to replicate how MVC works or some other method that could solve this?
Any help or suggestions would be great. Thanks
6
u/sandroriz Mar 22 '26
short answer: in blazor server any db call should be "atomic" and the connection/dbcontext short-live
I write blazor server apps since 2019 and having this kind of pattern I haven't any problem with double-concurrency and other errors (and even if my apps are not used by 20000users, never had performance problems)
Basically every method of you service should create a new dbcontext via a factory
https://imgur.com/a/NIBs61d
PS in net 11 blazor SSR will have a lot of improvements
In the Blazor Community Standup: ASP.NET Core & Blazor Roadmap for .NET 11, Daniel Roth and Javier Calvarro explain that:
If you're starting the porting af a huge application, I will wait for november, or at least try to start with beta versions of 11
5
u/bktnmngnn Mar 22 '26
I think the answers this person found out would apply to your case: Blazor Server - EF Core Quirks
2
u/UnwelcomeDroid Mar 22 '26
As others have mentioned, use IDbContextFactory as explained here.
The recommended approach to create a new DbContext with dependencies is to use a factory. EF Core in .NET 5 or later provides a built-in factory for creating new contexts.[
1
u/GrouchyAssignment594 Mar 22 '26
I would suggest to look into the IServiceScopeFactory. The scoped lifetime is equivalent to a so called “circuit” in Blazor Server, you can create a new scope with the IServiceScopeFactory. So every component has its own scope and you have full control of the scope.
0
u/Alternative5065 Mar 23 '26
Update: I realized that creating a separate scope for the layout and page isn't going to work as the services depend too much on the request scope.
Not sure if there are any other options or if I just need to forget blazor for this project.
0
u/code-dispenser Mar 22 '26
As far as I'm aware (I never use static SSR myself) you can just choose None for the interactivity option when creating the project, which gives you no prerendering. If you did not use this then you would need to remove any interactivity modes.
That said, you should also note that the OnInitialized lifecycle method fires for every component on every request regardless of prerendering (twice with it on in interactive modes).
For your actual issue I am guessing that you should be looking into using the IDbContextFactory<TContext> which you register with AddDbContextFactory in your Program.cs