ASP.NET Core Razor pages with Blazor components establish web socket connections even on pages that don’t need it

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// ...
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
// ...
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
</code>
<code>// ... builder.Services.AddRazorComponents().AddInteractiveServerComponents(); // ... app.MapRazorComponents<App>().AddInteractiveServerRenderMode(); </code>
// ...
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
// ...
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();

and adding the Blazor javascript reference to the _Layout.cshtml file.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script src="_framework/blazor.web.js"></script>
</code>
<code><script src="_framework/blazor.web.js"></script> </code>
<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:

  1. This is a new project, so all the code is new.
  2. 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:

  1. Home is a Server Side statically rendered page.
  2. Weather is Server Side statically rendered page that uses streaming.
  3. 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật