.NET 8 Blazor MAUI Hybrid编译报错:含应用主机EXE的打包应用不能为中性架构
升级到.NET 8后,执行dotnet publish命令时出现以下错误:
C:\Users\VssAdministrator.nuget\packages\microsoft.windowsappsdk\1.3.230724000\buildTransitive\Microsoft.Build.Msix.Packaging.targets(1036,5):
error : Packaged .NET applications with an app host exe cannot be
ProcessorArchitecture neutral. Please specify a RuntimeIdentifier or a
Platform other than AnyCPU.
即使指定了win-x64或win10-x64作为RuntimeIdentifier仍报错,可通过以下步骤解决:
修改publish命令,添加Platform参数
在原有命令中加入-p:Platform=x64,强制指定架构,修改后的命令如下:dotnet publish -f:net8.0-windows10.0.19041.0 -r:win-x64 -c:Release -o:$(build.artifactstagingdirectory) --no-self-contained -p:Platform=x64统一项目.csproj中的架构配置
打开项目的.csproj文件,确保架构配置一致,避免冲突:<PropertyGroup> <!-- 明确指定RuntimeIdentifier --> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <!-- 强制平台目标为x64,替代AnyCPU --> <PlatformTarget>x64</PlatformTarget> </PropertyGroup>移除项目中任何
<Platform>AnyCPU</Platform>的配置项。同步Azure DevOps Pipeline的构建平台设置
在Azure DevOps的Pipeline任务中,将构建平台从默认的Any CPU改为x64。例如VSBuild任务配置:- task: VSBuild@1 inputs: solution: '**/*.sln' platform: 'x64' configuration: 'Release'升级Windows App SDK包版本
将Microsoft.WindowsAppSDK包升级到最新稳定版(如1.4.x系列),新版本修复了.NET 8下的部分架构检测问题,修改.csproj中的包引用:<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.240211001" />
原因说明
.NET 8对MSIX打包应用的架构校验更严格,仅指定RuntimeIdentifier不足以覆盖默认的AnyCPU平台设置,必须同时明确Platform参数和项目配置中的架构目标,确保全流程架构一致。
内容的提问来源于stack exchange,提问作者ArturM

