如何解决Azure DevOps中旧Silverlight项目构建失败问题
问题背景
维护旧版Silverlight项目时,使用Azure DevOps的Windows 2019/2022镜像构建出现报错:
2022-09-10T14:26:05.9397408Z ##[error]abcWebSilverlight\abcWebSilverlight.csproj(1056,11): Error MSB4226: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\Silverlight\v5.0\Microsoft.Silverlight.CSharp.targets" was not found. Also, tried to find "Microsoft\Silverlight\v5.0\Microsoft.Silverlight.CSharp.targets" in the fallback search path(s) for $(MSBuildExtensionsPath32) - "C:\Program Files (x86)\MSBuild" . These search paths are defined in "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\msbuild.exe.Config". Confirm that the path in the
declaration is correct, and that the file exists on disk in one of the search paths.
核心原因是Windows 2019/2022镜像中的Visual Studio默认未预装Silverlight构建所需的SDK和targets文件。
可行解决方案
方案1:用Chocolatey快速安装Silverlight SDK
这是最简便的方式,通过包管理器直接安装Silverlight 5 SDK到构建代理:
- 在构建管道中添加PowerShell任务,设置为"Inline"模式
- 执行以下命令:
choco install silverlight-sdk -y
该命令会将Silverlight的targets文件安装到C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v5.0目录,匹配MSBuild的 fallback 搜索路径,直接解决文件缺失问题。
方案2:通过Visual Studio Installer添加Silverlight组件
如果需要和现有VS环境深度集成,可给镜像中的VS安装Silverlight相关组件:
- 在构建管道中添加Visual Studio Installer任务
- 配置参数:
- Product:选择镜像对应的Visual Studio版本(如Visual Studio Enterprise 2019)
- Workloads/Components:搜索并勾选
Silverlight 5 SDK和Silverlight Project System(或直接指定组件ID:Microsoft.VisualStudio.Component.Silverlight.Tools)
- 保存后运行管道,组件安装完成后再执行构建任务。
方案3:手动部署Silverlight Targets文件
若上述方法受环境限制,可手动复制所需文件到构建代理:
- 从本地装有Silverlight开发环境的机器上,复制
C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v5.0目录下的所有文件 - 将这些文件上传到Azure DevOps的安全文件或代码存储库
- 在构建管道中添加Download Secure File(或从存储库拉取)任务,获取文件包
- 添加PowerShell任务,执行复制命令:
Copy-Item -Path "下载的文件路径\*" -Destination "C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v5.0" -Recurse -Force
方案4:安装Silverlight Project System扩展
针对你提到的扩展,按以下步骤在构建代理中部署:
- 下载扩展的VSIX安装包,上传到Azure DevOps的安全文件
- 在构建管道中添加Download Secure File任务,下载该VSIX文件
- 添加VSIX Installer任务,配置:
- VSIX file path:选择下载后的VSIX文件路径
- Target Visual Studio version:选择镜像中的VS版本(2019或2022)
- 扩展安装完成后执行构建任务。
内容的提问来源于stack exchange,提问作者NemoDima

