You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Pipeline中构建Fake程序集并解决编译错误?

Azure Pipeline构建含Microsoft Fakes的C#测试项目解决方案

问题现象

本地可正常构建并运行使用Microsoft单元测试框架及Fakes技术的C#项目,但在Azure Pipeline中构建时出现以下警告和错误:

警告信息

Primary reference "___.Fakes". ##[warning]C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(2352,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "___.Fakes". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

错误信息

##[error]___\___Tests\___Tests.cs(9,43): Error CS0234: The type or namespace name 'Fakes' does not exist in the namespace 'Microsoft.AspNetCore.SignalR.Client' (are you missing an assembly reference?)

已尝试手动引用Microsoft.VisualStudio.TestPlatform.TestFramework.dll和Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll,以及添加MSBuild参数/p:EnableIntelliTrace=false,但问题未解决。

解决步骤

  • 使用VisualStudioBuild任务替代MSBuild任务
    Microsoft Fakes依赖Visual Studio的专属构建组件,普通MSBuild任务无法触发Fakes程序集的动态生成。在Pipeline中使用VisualStudioBuild任务,并指定与本地一致的VS版本(如VS2022对应版本号17.0):

    - task: VisualStudioBuild@1
      inputs:
        solution: '**/*.sln'
        vsVersion: '17.0'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
    
  • 通过NuGet管理Fakes依赖
    移除手动添加的测试框架DLL引用,在测试项目中安装Microsoft.QualityTools.Testing.Fakes NuGet包,确保Pipeline构建时自动下载并引用所需依赖。

  • 确认.fakes文件的构建动作
    检查测试项目中的.fakes配置文件(如Microsoft.AspNetCore.SignalR.Client.fakes),确保其Build Action设置为Fakes,该设置是动态生成Fakes程序集的必要条件。

  • 使用VS安装目录下的MSBuild(若坚持用MSBuild任务)
    如果必须使用MSBuild任务,需指定Visual Studio安装路径下的MSBuild可执行文件,并添加VisualStudioVersion参数:

    - task: MSBuild@1
      inputs:
        solution: '**/*.sln'
        msbuildLocation: 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe'
        msbuildArguments: '/p:VisualStudioVersion=17.0 /p:Configuration=$(buildConfiguration)'
    
  • 确保代理权限
    构建代理需拥有项目目录的写入权限,Fakes会在项目下生成FakesAssemblies目录存放动态生成的程序集,无写入权限会导致生成失败。

内容的提问来源于stack exchange,提问作者dgad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 11:11:32