无法加载文件或程序集 'Microsoft.Build.Framework'(VS 2017)

Could not load file or assembly 'Microsoft.Build.Framework'(VS 2017)

当我尝试 运行 命令 "update-database" 时,我得到了这个异常:

Specify the '-Verbose' flag to view the SQL statements being applied to the target database. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. File name: 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.`

我想我遇到了和你一样的问题。我没有保存整个错误信息,但我的错误信息是

'Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

我正在使用 Visual Studio 2017 并试图在 Add-Migration 之后执行 Update-Database

为了解决这个问题,我关闭了 Visual Studio 并重新打开它,然后重新运行 Update-Database

这可能会或可能不会解决您的问题,但我想我会 post 以防万一它会有所帮助。

此问题的根本原因来自 devenv.exe.config 文件中到 Microsoft.Build.Framework.dll 的相对路径(参见 xml 标记)。

一些 Visual Studio 扩展正在更改当前目录并使相对路径无效。

要修复它,请在 C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\Common7\IDE\ 目录中打开此文件。并将所有 ..\..\MSBuild.0\Bin\ 替换为 C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\MSBuild.0\Bin\.

我们遇到了这个问题,以下是我们需要在我们的案例中做的事情:

问题是我们配置了一个调用 HttpRuntime.Cache["somekey"] 的数据库命令拦截器 (IDbCommandInterceptor),由于某些原因,迁移命令无法 运行。删除此依赖项后,所有命令 运行 完美。也许 HttpRuntime 找不到 Build Framework dll?

所以当迁移命令失败时检查整个callstack 看看你是否有类似的问题。

我找到了一个似乎可以永久解决问题的解决方法,至少在我的环境中如此 运行 VS 2017 Professional 15.5.2 和 Entity Framework 6.1.1。

基本上,将 DLL(以及一些相关的)安装到 GAC(全局程序集缓存)中,问题就会消失。

按照以下步骤操作:

  1. 关闭 Visual Studio 2017

  2. 的所有 运行 个实例
  3. 启动 Visual Studio 2017 开发者命令提示符

  4. 键入以下命令(将 Professional 替换为您的版本,Enterprise 或 Community,或相应地调整路径):

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Build.Framework.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Build.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Build.Engine.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Build.Conversion.Core.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Build.Tasks.Core.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Build.Utilities.Core.dll"
  1. 重启Visual Studio 2017

本质上,当 .NET 尝试加载 DLL 时,GAC 将(在大多数情况下)获得优先权,并且 FileNotFoundException 将消失,因为您的 DLL 现在将通过 GAC 解析。

同样,它对我有用,它只是一种解决方法,它本身不会解决核心问题,但至少我在尝试使用 EF 迁移时不必一直重启 VS,这就是对我来说足够好了。

以防万一重新启动 Visual Studio 不起作用转到任务管理器/进程资源管理器和技能 VBCSCompiler.exe

建议使用Process Explorer

我缺少的文件或程序集版本与问题不同。

我在尝试发布我的 ASP.net 项目时出现此错误

Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

我通过安装 Microsoft Build Tools 2015

解决了这个问题

我认为我的问题是因为我在 VS 2017 中发布了使用 VS 2015 构建的项目。希望可以帮助其他有同样问题的人。

关闭重新打开 Visual Studio非常有效!

感谢那些已经发帖的人。我的情况是通过上述组合解决的。我有几个版本的 Visual Studio:2015、2017、2019。在某个时候,MSBUILD 的版本从 15.1 变为 15.9,我通过更新 C:\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe.config 文件以指向 15.9 解决了这个问题图书馆。以下是其中一项的示例:

<dependentAssembly>
      <assemblyIdentity name="Microsoft.Build.Utilities.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
      <bindingRedirect oldVersion="2.0.0.0-99.0.0.0" newVersion="15.9.0.0"/>
      <codeBase version="15.9.0.0" href="..\..\MSBuild.0\Bin\Microsoft.Build.Utilities.Core.dll" />
</dependentAssembly>

我在 macOS 上更新 XCode/Mono 组件时遇到了同样的问题。

解决方案是将 Mac 的 Visual Studio 更新到最新版本。

我认为这个问题是使用 .NET Core 3.0 包中的新 MSBuild 工具引起的,该包安装了新的 XCode/Mono 版本。

就我而言,某些东西(可能是 NuGet 更新)确实将 AssemblyBinding 添加到 web.config-File:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
</dependentAssembly>

删除该 dependentAssemby-Entry 后,我可以再次发布项目。

我们的本地构建脚本使用旧版本的 nuget.exe (4.7.1.5393) 来恢复 NuGet 包。我们在更新到 Visual Studio 2019 版本 16.5.0 后开始收到此错误。更新到最新版本的 nuget.exe (5.4.0.6315) 为我们解决了这个问题。

nuget.exe 可以在这里下载:https://www.nuget.org/downloads.

这对我有用 - 从 2020 年开始似乎是一个非支持问题。

Azure Build Pipeline > NuGet tool installer 步骤中,将 Version of NuGet.exe to install 更改为较新的版本,例如 5.4.0。在 https://dist.nuget.org/tools.json.

检查版本

问题已消失,现在构建成功。

这对我有用: 当我执行 nuget restore 命令时发生错误。 Nuget 版本 4.6.2。我有两种方法可以解决这个问题。

使用 Nuget 4.8.2 及更高版本。 gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Microsoft.Build.Framework.dll

使用 Visual Studio 2019 社区版。我尝试了其他解决方案但运气不佳,但在清除 NuGet 缓存后问题似乎得到了解决。

在尝试了上述所有方法以及更多方法后 - 运行 WCF 应用程序对我来说仍然失败。错误:Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0..

note: tried Restarting VS, PC, killing processes, cleaning up the VS caches, nuget caches, obj, bin, .vs, packages folders

对我有用的是删除项目的 *.csproj.user 文件。显然它有一些过时的配置。花了 4 个小时试图弄明白..

我重新启动,然后我发现我一直在使用的本地 Web 服务 运行 被占用该端口的另一个进程阻止了。我检查了进程 运行 并使用 TCPView 终止了进程,一切似乎又开始工作了。

我的 web.config 中有这部分,只是删除它然后它开始工作:)

<compilation debug="true" targetFramework="4.5.2">
    <assemblies>
        <add assembly="Microsoft.Build.Framework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    </assemblies>
</compilation>

调用“MSBuildLocator.RegisterDefaults();”在访问 DLL 之前对我有用。

问题:无法加载文件或程序集'Microsoft.Build.Framework...

解决方案:我在我的项目中引用了 Microsoft.Build.Framework Nuget 包,并为此 NuGet 包设置了引用 Copy Local True,问题已为我解决。

在我的 Roslyn 项目中,引用 NuGet 包 Microsoft.Build.Locator 并在创建 MSBuildWorkspace 之前调用 MSBuildLocator.RegisterDefaults(); 解决了这个问题。

我必须使用 .NET cross-platform 开发选项安装 visual studio。 我也得到了通用 windows 平台开发

https://docs.microsoft.com/en-us/visualstudio/install/install-visual-studio?view=vs-2022

在装有 .NET 6 的 macOS 上,我在将表单 6.0.1 升级到 6.0.2 后开始看到此错误。修复方法是卸载并重新安装 dotnet-ef cli:

dotnet tool uninstall --global dotnet-ef
dotnet tool install --global dotnet-ef