如何让Roslyn/MSBuildWorkspace与VS/MSBuild行为一致?(ResolveComReferences问题)
问题描述
编译行为差异
在Visual Studio或直接调用MSBuild.exe编译包含无效COM引用的旧版.NET Framework项目时,仅会产生警告,项目仍能正常编译完成:
2>------ Rebuild All started: Project: ResolveComReference, Configuration: Debug Any CPU ------ 2>C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(2977,5): warning MSB3284: Cannot get the file path for type library "ffffffff-0000-0000-0000-0000000fffff" version 3.0. Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)) 2>C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(2977,5): warning MSB3305: Processing COM reference "VAccessLib" from path "C:\Program Files (x86)\Actian\Zen\bin\ACBtr732.ocx". Type library importer has encountered an interface not derived from IUnknown: 'ISWData'. 2> ResolveComReference -> C:\ResolveComReference\bin\Debug\ResolveComReference.dll
MSBuildWorkspace调用失败
使用代码调用MSBuildWorkspace加载解决方案时直接失败,代码示例:
workspace = MSBuildWorkspace.Create(); var solution = await workspace.OpenSolutionAsync(solutionPath);
报错信息:
Msbuild failed when processing the file 'C:\ResolveComReference\ResolveComReference.csproj' with message: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets: (2977, 5): The "ResolveComReference" task could not be instantiated from "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Could not load file or assembly 'Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Msbuild failed when processing the file 'C:\ResolveComReference\ResolveComReference.csproj' with message: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets: (2977, 5): The "ResolveComReference" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.
额外的项目引用问题
同时存在项目引用匹配失败的问题:
Found project reference without a matching metadata reference: C:\TestProjects\_EmptyProject48\code.csproj
当前环境与尝试
- 问题出在使用「独立代码分析工具」项目模板创建的代码中
- 更新NuGet包无法解决问题
- 可通过
AdhocWorkspace手动解析sln和csproj文件,但更希望让MSBuildWorkspace的表现与MSBuild.exe一致
示例无效COMReference项
<COMReference Include="some_unexisting_library"> <Guid>{FFFFFFFF-0000-0000-0000-0000000FFFFF}</Guid> <VersionMajor>3</VersionMajor> <VersionMinor>0</VersionMinor> <Lcid>0</Lcid> <WrapperTool>aximp</WrapperTool> <Isolated>False</Isolated> </COMReference>
内容的提问来源于stack exchange,提问作者Timur Kelman
相关产品推荐
相关产品推荐

