'Best practices for using components with a Blazor WebAssembly app if it is ASPNET Core Hosted?

I have a functioning Blazor WebAssembly (WASM) component which is ASP.NET Core Hosted and contains three projects: client, server, and shared. The client-side component connects to a SQL Server database using Web API endpoints contained in a controller file within the server-side app.

I would now like to share this component with multiple apps. Would the best practice be to use a Razor Class Library (RCL) or to just keep the component in project format and directly add/reference it within projects that will use it? If an RCL is recommended, how do I handle the controller?



Solution 1:[1]

I found a partial answer to my question. It appears that prior to .NET 6 it was possible to share Blazor components by sharing their projects. However, with .NET 6 this now generates an error and Razor Class Library's (RCLs) are the recommended method for sharing WebAssembly components. Source: GitHub aspnetcore issue #36732

I also posted to MSDN and to date have been unable to find a way to combine client-side WASM classes and server-side controller classes into a single RCL. From what I have learned so far, I can see two options:

  1. Use an RCL for the client-side component and a regular class library (or second RCL) for the server-side controller. (I have not tested this).

  2. Convert to a server-side Blazor project. This is the solution I ended up using. It does not require a Web API/controller and sharing components via RCL works well. Bonus, coding/debugging is easier and the project loads faster. This solution is working well for my present needs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1