如何从.NET 7 WPF应用中启动ASP.NET Core并托管Blazor WASM
WPF托管ASP.NET Core提供Blazor WASM服务的环境变量与资源加载问题
场景与已尝试操作
我当前的项目是运行在硬件上的.NET 7 WPF应用,需要通过WPF托管ASP.NET Core应用来提供Blazor WASM服务,实现让他人查看应用部分内容的需求。由于要共享内存和硬件资源,无法将ASP.NET Core拆分为独立可执行文件。
已完成以下操作:
- 在同一解决方案中创建由ASP.NET Core托管的Blazor WASM应用
- 在WPF应用中添加对ASP.NET Core项目的引用
- 通过静态类
WebServer启动服务端
WebServer类代码
public class WebServer : IAsyncDisposable { private static WebServer? instance; private readonly CancellationTokenSource cancellationTokenSource; private WebApplication? theApp; public static WebServer Instance { get { instance ??= new WebServer(); return instance; } } private WebServer() { cancellationTokenSource = new CancellationTokenSource(); } public async Task<bool> StartWebServer(string[] args, ViewModelManager viewModelManager) { if (theApp != null)return false ; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddSingleton(_ => viewModelManager.ViewMeasurement); theApp = builder.Build(); if (theApp.Environment.IsDevelopment()) { theApp.UseWebAssemblyDebugging(); } else { theApp.UseExceptionHandler("/Error"); theApp.UseHsts(); } theApp.UseHttpsRedirection(); theApp.UseBlazorFrameworkFiles(); theApp.UseStaticFiles(); theApp.UseRouting(); theApp.MapApiEndpoints(); theApp.MapFallbackToFile("index.html"); await theApp.RunAsync(cancellationTokenSource.Token); return true; } public ValueTask DisposeAsync() => theApp?.DisposeAsync() ?? ValueTask.CompletedTask; public async Task StopWebServer() { cancellationTokenSource.Cancel(); await (theApp?.StopAsync() ?? Task.CompletedTask); } }
WPF启动服务代码
Task.Run(async () => await WebView.Server.WebServer.Instance.StartWebServer(Array.Empty<string>(), viewModelManager));
现有问题
- 服务无法识别任何ASP环境变量,默认在端口5000启动
- 完全无法提供WASM内容
解决方案
1. 手动指定环境与端口(无需环境变量)
直接在构建WebApplication时硬编码或从WPF配置读取环境和端口,绕过环境变量依赖:
public async Task<bool> StartWebServer(string[] args, ViewModelManager viewModelManager) { if (theApp != null) return false; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); // 手动设置运行环境(可选:Development/Production/Staging) builder.Environment.EnvironmentName = "Development"; // 手动指定服务监听端口,替换为你需要的端口 builder.WebHost.UseUrls("http://localhost:5001", "https://localhost:5002"); builder.Services.AddSingleton(_ => viewModelManager.ViewMeasurement); theApp = builder.Build(); // 后续管道配置保持不变... }
2. 确保Blazor WASM静态资源被正确加载
由于WPF引用ASP.NET Core项目,需要确保ASP.NET Core的wwwroot资源能被WPF的输出目录访问:
步骤1:修改ASP.NET Core项目的csproj
添加以下配置,确保wwwroot下的所有文件在构建时复制到输出目录:
<Project Sdk="Microsoft.NET.Sdk.Web"> <!-- 其他现有配置 --> <ItemGroup> <Content Update="wwwroot/**/*"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> </Project>
步骤2:在WebServer中指定静态文件路径
修改UseStaticFiles的配置,明确指向输出目录下的wwwroot:
// 替换原有的theApp.UseStaticFiles(); var wwwrootPath = Path.Combine(AppContext.BaseDirectory, "wwwroot"); theApp.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(wwwrootPath) });
3. 验证Blazor Framework Files配置
确保UseBlazorFrameworkFiles()在UseStaticFiles()之前调用(你的现有代码已经符合这个顺序,只需确认静态资源路径正确即可)。
内容的提问来源于stack exchange,提问作者Foitn
相关产品推荐
相关产品推荐

