如何在Azure DevOps Pipeline中构建含Concerto4xSecurity COM引用的ASP.NET Core 8 Web API
针对你遇到的error MSB4803: The task "ResolveComReference" is not supported on the .NET Core version of MSBuild错误,以下是几个可行的构建方案:
方案1:使用Windows代理+完整.NET Framework版MSBuild
由于ResolveComReference任务仅支持.NET Framework版本的MSBuild,需在Azure DevOps Pipeline中配置Windows虚拟机代理,并指定使用Visual Studio安装目录下的.NET Framework版MSBuild执行构建:
- 在Pipeline YAML中替换原构建任务为
MSBuild任务,指定MSBuild的完整路径(以VS2022为例):
- task: MSBuild@1 inputs: solution: '**/*.sln' msbuildArchitecture: 'x64' msbuildArguments: '/p:Configuration=Release /p:Platform="Any CPU"' msbuildPath: 'C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe'
- 确保代理虚拟机已安装对应版本的Visual Studio(需包含.NET Framework开发工具集)。
方案2:预生成互操作程序集(PIA)替代直接COM引用
提前生成COM组件的互操作程序集,将其作为项目依赖文件引入,避免构建时触发ResolveComReference任务:
- 在本地环境或专用构建机器上,使用
tlbimp.exe(位于.NET Framework SDK目录,如C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\)生成互操作dll:
tlbimp Concerto4xSecurity.tlb /out:Interop.Concerto4xSecurity.dll /namespace:Concerto4xSecurity
将生成的
Interop.Concerto4xSecurity.dll添加到项目源码目录,在ASP.NET Core项目中移除原COM引用,改为直接引用该dll,并设置复制本地为True。后续构建时,直接使用
dotnet build即可,无需处理COM引用解析。
方案3:拆分项目,隔离COM引用至.NET Framework类库
将依赖COM组件的代码封装到单独的.NET Framework类库项目中,ASP.NET Core项目通过引用该类库实现功能调用,拆分后分别处理构建:
创建新的.NET Framework类库项目(如.NET Framework 4.8),将原ASP.NET Core项目中涉及
Concerto4xSecurity的代码迁移至此,并添加COM引用。在Azure DevOps Pipeline中,先使用.NET Framework版MSBuild构建该类库项目,再用
dotnet build构建ASP.NET Core主项目:
# 构建.NET Framework类库 - task: MSBuild@1 inputs: solution: '**/ComDependencyLibrary.csproj' msbuildPath: 'C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe' msbuildArguments: '/p:Configuration=Release' # 构建ASP.NET Core Web API - task: DotNetCoreCLI@2 inputs: command: 'build' projects: '**/YourWebApi.csproj' arguments: '--configuration Release'
内容的提问来源于stack exchange,提问作者Tahami Rizwan

