使用MSBuild生成MSIX报“无目标”错误,但Visual Studio可正常构建
问题:MSBuild打包WPF到MSIX失败,Visual Studio中可正常执行
我将三个WPF应用整合到MSIX包中,解决方案包含这三个WPF应用、一个共享项目,以及一个「Windows应用打包项目」。在Visual Studio中选择打包项目的「发布→创建应用包」,选择侧载、自签名证书,配置为Release(Any CPU)时,能正常生成MSIX输出。但用MSBuild命令行执行相同操作以集成到自动化构建系统时,WPF应用报错。
使用的MSBuild命令
MSBuild.exe /restore /p:AppxBundle=Never /p:UapAppxPackageBuildMode=Sideloading /p:Configuration=Release;Platform="Any CPU" /p:AppxPackageDir=C:\temp\Output\ /p:AppxPackageSigningEnabled=false
错误信息
:\Repos\app\app.sln" (default target) (1:2) - "C:\Repos\app\appPackage\appPackage.wapproj" (default target) (19:6) - "C:\Repos\app\app\app.csproj" (publish target) (2:18) - C:\Program Files\dotnet\sdk\6.0.107\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1047: Assets file 'C:\Repos\app\app\obj\project.assets.json' doesn't have a target for 'net6.0-windows10.0.19041/win-x86'. Ensure that restore has run and that you have included 'net6.0-windows10.0.19041' in the TargetFrameworks for your project. You may also need to include 'win-x86' in your project's RuntimeIdentifiers. [C:\Repos\app\app\app.csproj]
原因分析
Visual Studio在打包MSIX时,会自动处理平台与运行时标识符(RID)的映射逻辑:当选择Any CPU时,会默认适配打包项目的目标平台(多数情况下MSIX打包项目默认目标为x86)。但直接使用MSBuild命令行时,Platform="Any CPU"的参数传递方式存在歧义,且未明确指定运行时标识符,导致构建流程尝试以win-x86编译WPF项目,但项目在restore阶段未生成对应RID的资产文件,最终触发NETSDK1047错误。
解决方案
1. 修正MSBuild命令参数
调整平台参数,或明确指定运行时标识符,让打包项目驱动正确的构建逻辑:
- 直接指定平台为x86(匹配MSIX打包项目默认目标平台):
MSBuild.exe /restore /p:AppxBundle=Never /p:UapAppxPackageBuildMode=Sideloading /p:Configuration=Release /p:Platform=x86 /p:AppxPackageDir=C:\temp\Output\ /p:AppxPackageSigningEnabled=false
- 保留Any CPU并明确指定RuntimeIdentifier:
MSBuild.exe /restore /p:AppxBundle=Never /p:UapAppxPackageBuildMode=Sideloading /p:Configuration=Release /p:Platform="Any CPU" /p:RuntimeIdentifier=win-x86 /p:AppxPackageDir=C:\temp\Output\ /p:AppxPackageSigningEnabled=false
2. 调整WPF项目配置(可选)
若希望项目默认支持多运行时标识符,可在WPF项目的.csproj文件中添加如下配置:
<PropertyGroup> <RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers> </PropertyGroup>
这样执行restore时会自动生成对应RID的资产文件,避免构建时出现目标缺失问题。
内容的提问来源于stack exchange,提问作者Matthew van Boheemen
相关产品推荐
相关产品推荐

