I’m going to be writing an application with thousands of concurrent users. The majority of the pages in this application are standard CRUD pages that a regular ASP.NET Core Razor pages app will be just fine for.
There are only a couple of pages where I need the full page interactivity and richness of a Blazor server side rendered app, so I don’t want to incur the overhead of a persistent web socket connection to the server on these other pages.
I decided that the theoretical perfect architecture would be to create an ASP.NET Core Razor pages app with a reference to a Razor class library. The Razor class library would then have the components that need to have the Blazor functionality, and I would simply use those components of the few pages of the application that need it. My hope was that the web socket connection would be established JUST for the pages that are hosting one of the Razor (Blazor) components and the rest of the pages would not need, or have, the web socket connection at all.
However, that’s not what happens. The second I spin up the Razor pages app to the home page, which has no Razor components on it, the Blazor web socket connection is immediately established.
I followed the procedure on this Microsoft article for setting this up:
https://learn.microsoft.com/en-us/aspnet/core/blazor/components/integration?view=aspnetcore-8.0
It explains that I have to add Interactive Server Components to the Razor Pages app by adding these lines to the Program.cs
file:
// ...
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
// ...
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
and adding the Blazor javascript reference to the _Layout.cshtml
file.
<script src="_framework/blazor.web.js"></script>
I assumed that it would establish web sockets when needed, but I’m fairly certain that these registration are what’s causing the immediate web socket connection for the whole app. Isn’t there any way to establish the connection on-demand for just the pages that require it and stay completely “vanilla razor pages” everywhere else?
Thanks in advance for the help!
1
I assumed that it would establish web sockets when needed, but I’m fairly certain that these registration are what’s causing the immediate websocket connection for the whole app
Not true. If you set things up correctly it does what you think it should do.
I’m making some assumptions:
- This is a new project, so all the code is new.
- It’s Net8.
Add a solution use the Blazor Web App Template. Select InteractiveServer and Per Page/Component interactivity.
You get a project with 3 pages:
- Home is a Server Side statically rendered page.
- Weather is Server Side statically rendered page that uses streaming.
- Counter is an interactively Rendered page.
In all cases Routes
and MainLayout
are rendered statically on the server. The Body
content component only renders interactively when it’s set to do so.
Debug the solution and look at the Developer tools in the browser. Only the Counter
page establishes a WebSocket connection which get’s closed when you navigate away from the interactive page.
See the screen captures below:
Home – no web socket connection.
Counter – active web socket connection
Weather – no active web socket – marked as completed.
All of your static CRUD pages should be built like Home or Weather. Your few interactive pages like Counter
. You can move stuff into separate libraries, but the same basic rules apply. Just ensure the router can see the assemblies to trawl for routes.
5