I am currently working on extending my WPF app with a Blazor web interface to allow users to configure some settings through their browser. To achieve this, I launch a Blazor server app from within my WPF app using WebApplicationBuilder.
WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions{ Args = args});builder.Services.AddRazorComponents() .AddInteractiveServerComponents();_app = builder.Build();// Configure the HTTP request pipeline.if (!_app.Environment.IsDevelopment()){ _app.UseExceptionHandler("/Error", createScopeForErrors: true); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. _app.UseHsts();}_app.UseHttpsRedirection();_app.UseStaticFiles();_app.UseAntiforgery();_app.MapRazorComponents<App>() .AddInteractiveServerRenderMode();// Runawait _app.RunAsync(_cancellationTokenSource.Token);With this approach, the Blazor app starts fine and loads successfully. However the static (CSS, JS) files, including the{project}.styles.css are not loaded, because the wwwroot folder doesn't exist in the WPF app's bin directory.
To fix this, I updated the WebApplicationBuilder to manually configure the Blazor's content root path:
string path = "Path to blazor project";WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions{ Args = args, ContentRootPath = path, WebRootPath = "wwwroot"});This change allows the static files from the Blazor project to load correctly. However,{project}.styles.css and the static files from referenced Razor Class Libraries or nuget packages still don't load, this is because the wwwroot folder is still missing the {project}.styles.css and the _content folder.
After some more looking around, I discovered that publishing the Blazor project on it's own generates a wwwroot folder with all the required files. I updated my WPF project so it copies the wwwroot folder from the published Blazor app and now everything seems to be working.
However, this isn't an ideal solution for development, since publishing the Blazor app after each change is quite annoying.
So I was wondering if there is a better way of making this work?