DevOps管道发布WinUI3应用时WebView2空白页:32位DLL所致
确保Azure DevOps管道构建WinUI3应用时使用64位WebView2的解决方案
针对你遇到的本地X64构建正常、但Azure DevOps发布后WebView2.Core.dll为32位的问题,可以通过以下步骤解决:
1. 强制项目文件锁定64位架构
打开你的MyApp.csproj文件,在<PropertyGroup>节点中添加或修改配置,明确指定x64平台和运行时:
<PlatformTarget>x64</PlatformTarget> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <PublishSingleFile>false</PublishSingleFile> <SelfContained>true</SelfContained>
同时确认WebView2的包引用无多余条件限制:
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2045.28" />
2. 拆分Azure DevOps管道的构建流程
直接使用publish命令可能跳过平台相关的包还原和构建逻辑,建议拆分为restore、build、publish三个独立步骤,确保每一步都明确指定x64参数:
修改后的管道配置:
pool: vmImage: 'windows-latest' variables: solution: 'MySolution/MySolution.sln' project: 'MySolution/MyApp/MyApp.csproj' buildPlatform: 'x64' buildConfiguration: 'Release' runtimeIdentifier: 'win-x64' steps: # 还原对应运行时的NuGet包 - task: DotNetCoreCLI@2 inputs: command: 'restore' projects: '$(project)' arguments: '--runtime $(runtimeIdentifier)' # 以x64平台构建项目 - task: DotNetCoreCLI@2 inputs: command: 'build' projects: '$(project)' arguments: '--configuration $(buildConfiguration) --platform $(buildPlatform) --runtime $(runtimeIdentifier) --no-restore' # 发布时复用已构建的x64产物 - task: DotNetCoreCLI@2 inputs: command: 'publish' publishWebProjects: false projects: '$(project)' arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory) --platform $(buildPlatform) --runtime $(runtimeIdentifier) -p:PublishSingleFile=false --self-contained true --no-build' zipAfterPublish: true modifyOutputPath: false
3. 强制复制WebView2的x64原生文件(可选)
如果上述步骤仍未生效,可以在csproj中添加自定义目标,确保发布时复制win-x64版本的WebView2原生文件:
<Target Name="CopyWebView2NativeAssets" AfterTargets="Publish"> <Copy SourceFiles="$(NuGetPackageRoot)microsoft.web.webview2/1.0.2045.28/runtimes/win-x64/native/*" DestinationFolder="$(PublishDir)runtimes/win-x64/native/" SkipUnchangedFiles="true" /> </Target>
问题根源说明
之前添加-r win-x64无效,主要原因是:
- 项目未明确设置
PlatformTarget,默认以AnyCPU模式构建,优先拉取32位WebView2依赖; - 直接publish时未提前还原对应运行时的NuGet包,管道无法获取x64版本的WebView2文件。
内容的提问来源于stack exchange,提问作者esmorun
相关产品推荐
相关产品推荐

