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

GitLab CI运行.NET 4.6.1测试报错找不到mono主机如何解决?

问题根因

你的项目指定了TargetFramework为net461(即.NET Framework 4.6.1),属于Windows原生的.NET Framework生态。你在CI中使用的mcr.microsoft.com/dotnet/sdk:5.0是Linux平台的.NET SDK镜像,Linux环境下运行.NET Framework的测试用例依赖mono组件,而该官方镜像默认没有预装mono,因此触发了找不到mono的报错。你本地是Windows环境,原生支持.NET Framework运行时,所以执行命令不会报错。

解决方案

方案1:使用Windows GitLab Runner(最推荐,无需修改项目)

如果你有可用的Windows节点的GitLab Runner,直接更换build阶段的镜像为Windows版本的.NET SDK镜像即可,Windows环境原生支持.NET Framework,无需额外安装依赖。
修改.gitlab-ci.yml中build job的镜像配置:

build:
  stage: build
  # 换成Windows版本的.NET 5 SDK镜像
  image: mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019
  script:    
    - cd TestRegressionNebra/   
    - dotnet restore "TestRegressionNebra.csproj"
    - dotnet build "TestRegressionNebra.csproj"       
    - 'dotnet test "TestRegressionNebra.csproj" --test-adapter-path:. --logger:"junit;LogFilePath={assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'
  artifacts:
        when: always
        paths: 
            - ./**/*test-result.xml
        reports:
            junit: 
                - ./**/*test-result.xml    
  # 对应你的Windows Runner标签,如果原标签nebra就是Windows的可以不用改
  tags:
    - nebra-windows

方案2:Linux Runner环境预装mono

如果只能使用Linux Runner,可以选择已经预装了mono + .NET SDK的自定义镜像,或者在脚本中增加安装mono的步骤。示例配置:

build:
  stage: build
  image: mcr.microsoft.com/dotnet/sdk:5.0
  script:
    # 安装mono依赖
    - apt update && apt install -y apt-transport-https dirmngr gnupg ca-certificates
    - apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
    - echo "deb https://download.mono-project.com/repo/debian stable-buster main" | tee /etc/apt/sources.list.d/mono-official-stable.list
    - apt update && apt install -y mono-devel
    - cd TestRegressionNebra/   
    - dotnet restore "TestRegressionNebra.csproj"
    - dotnet build "TestRegressionNebra.csproj"       
    - 'dotnet test "TestRegressionNebra.csproj" --test-adapter-path:. --logger:"junit;LogFilePath={assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'
  artifacts:
        when: always
        paths: 
            - ./**/*test-result.xml
        reports:
            junit: 
                - ./**/*test-result.xml    
  tags:
    - nebra

该方案会增加流水线执行时间,建议自行构建预装了mono和.NET SDK的镜像使用,避免每次运行都重复安装依赖。

方案3:修改项目为多目标框架(无需调整CI环境)

如果你可以修改项目配置,可以将项目调整为同时支持.NET Framework和.NET 5,测试时直接运行.NET 5版本的用例,无需依赖mono。修改.csproj文件的TargetFramework配置为TargetFrameworks:

<PropertyGroup>
    <!-- 同时支持net461和net5.0 -->
    <TargetFrameworks>net461;net5.0</TargetFrameworks>
    <IsPackable>false</IsPackable>
</PropertyGroup>

执行测试时指定目标框架为net5.0即可:

script:
  - cd TestRegressionNebra/   
  - dotnet restore "TestRegressionNebra.csproj"
  - dotnet build "TestRegressionNebra.csproj"       
  - 'dotnet test "TestRegressionNebra.csproj" -f net5.0 --test-adapter-path:. --logger:"junit;LogFilePath={assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 18:39:02