I'm trying to run a Blazor application (let's call it MyApp.Blazor) from a non-Blazor project that is also doing other things (MyApp.Service).
Non-viable solutions
Of course, the initial thought would be to have MyApp.Blazor be a standalone executable of which MyApp.Service isn't aware, and have the service just start it as another process. This isn't viable, as the web app needs two-way communication with the service.
The logical solution to that problem would be to expose an API from MyApp.Service, run MyApp.Blazor as a completely separate process, and have it make API requests to the service, however (for reasons that are beyond me and that I am currently unable to ask about), we're trying to avoid that approach.
To get specific, my requirement is to run a Blazor web application from a service application with 2-way communication via code. Thus, hosting a Blazor app from a non-Blazor project to achieve 2-way communication between site and service isn't entirely an XY problem.
What I've Tried
I created a new Blazor application (from the fluent UI template) and emptied out Program.cs. I created a static method which returns a new WebApplication, using the existing code from Program.cs (but not calling app.Run()).
Mapping static assets
I then call that factory method from the service project, and call Run(). This caused a runtime error in app.MapStaticAssets(), as it was looking for a file named for the entry assembly, instead of the executing assembly. I ended up fixing that issue by passing in the path explicitly:
string executingAssemblyDirectory= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new Exception("Could not find directory of executing assembly"); string staticWebAssetsEndpointsJson = Path.Join(executingAssemblyDirectory, "MyApp.Blazor.staticwebassets.endpoints.json"); app.MapStaticAssets(staticWebAssetsEndpointsJson);WebRoot not found
Upon starting up the app, then, it logs the following warnings:
warn: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[16] The WebRootPath was not found: C:\Users\...\MyApp.Service\bin\Debug\net9.0\wwwroot. Static files may be unavailable.warn: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[16] The WebRootPath was not found: C:\Users\...\MyApp.Service\bin\Debug\net9.0\wwwroot. Static files may be unavailable.When I hit localhost:5000, I get a page with no styling, and the "An unhandled error has occured. Reload X" message at the bottom. Checking the console:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)localhost/:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSONIndeed, there is not wwwroot folder. After reading this question, I tried adding the following to the service's project file:
<ItemGroup><Content Include="..\..\MyApp.Blazor\wwwroot\**"><CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory></Content></ItemGroup>Not only did this not copy across the wwwroot directory and its contents, I also gather that some files are generated at compile time and put into the webroot folder under the bin folder, so the contents would be incomplete even if it had worked.
Question
This is where I'm up to now, writing this question. What I'm doing feels convoluted and I'm convinced that I've gone wrong somewhere down the line.
So, the question is: How should I go about running a Blazor WebApplication as part of a non-Blazor project?