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

Docker中构建C#项目时出现Microsoft.Build.Framework程序集未找到异常的问题求助

Docker中构建C#项目时出现Microsoft.Build.Framework程序集未找到异常的问题求助

大家好,我在Docker环境下构建C#项目时遇到了一个棘手的问题,始终提示找不到Microsoft.Build.Framework程序集,折腾了好久都没解决,想请各位帮忙看看。

具体抛出的异常信息如下:

Unhandled exception. System.IO.FileNotFoundException: Could not load
file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot
find the file specified.

File name: 'Microsoft.Build.Framework, Version=15.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'    at
Microsoft.Build.Logging.StructuredLogger.CompilerInvocationsReader.Read(String
binLogFilePath)    at Broslyn.CSharpCompilationCapture.Build(String
projectFileOrSolution, Dictionary2 properties)    at Broslyn.CSharpCompilationCapture.Build(String projectFileOrSolution, String configuration, String platform, Dictionary2 properties)    at
Program.<Main>(String[] args)

我尝试添加dotnet add package Microsoft.Build.Locator --version 1.6.10这个包后,又出现了另一种错误。下面是完整的复现步骤和代码,希望能帮大家定位问题:

复现步骤

执行以下Docker构建命令即可重现问题:

sudo docker build -f - --no-cache -t test . <<EOF
FROM mcr.microsoft.com/dotnet/sdk:6.0.400

WORKDIR /tmp

RUN git clone --depth 1 --branch 13.0.3 https://github.com/JamesNK/Newtonsoft.Json.git /tmp/dep

WORKDIR /app

RUN dotnet new console && dotnet add package Broslyn --version 1.2.0

#RUN dotnet add package Microsoft.Build.Locator --version 1.6.10

RUN dotnet add package Microsoft.Build.Framework --version 17.5.0

COPY Program.cs .

CMD ["dotnet", "run", "/tmp/dep/Src/Newtonsoft.Json/Newtonsoft.Json.csproj"]
EOF

docker run test

用到的Program.cs代码

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Broslyn;

var compilation = await CSharpCompilationCapture.Build(args[0])
.Workspace
.CurrentSolution
.Projects.FirstOrDefault()!
.GetCompilationAsync();

备注:内容来源于stack exchange,提问作者ravid

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 12:59:34