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

构建服务器编译版本出现Microsoft.Extensions.Logging.Abstractions文件未找到异常

解决构建服务器编译版本抛出的Microsoft.Extensions.Logging.Abstractions文件未找到异常

本地编译的程序运行正常,但构建服务器编译的版本在本地运行时抛出以下异常:

Unhandled exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
File name: 'Microsoft.Extensions.Logging.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<GetOrAdd>g__BuildServiceProvider|4_1(IDbContextOptions _, ValueTuple`2 arguments)
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c.<GetOrAdd>b__4_0(IDbContextOptions contextOptions, ValueTuple`2 tuples)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
   at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
   at Microsoft.EntityFrameworkCore.DbContext..ctor()
   at MyProject.Models.DbContext..ctor() in c:\path\to\ci\pipeline\MyProject\Models\DbContext.cs:line 12

排查发现:解决方案内多个项目引用了Microsoft.EntityFrameworkCore,部分依赖的NuGet包引入了该程序集的2.0.0版本并被输出到目录,但Visual Studio未检测到NuGet版本冲突,问题大概率与CopyLocal属性配置、版本依赖不一致有关。

解决方案步骤

  • 统一NuGet包版本:在解决方案级别管理NuGet包,确保所有项目引用的Microsoft.Extensions.Logging.Abstractions版本统一为7.0.0,从根源避免跨版本依赖冲突。
  • 检查CopyLocal属性:针对每个项目中引用的Microsoft.Extensions.Logging.Abstractions程序集,右键查看属性,确认Copy Local设置为True,保证构建时将正确版本的程序集复制到输出目录。
  • 清理缓存并重建:
    • 本地执行命令清理NuGet缓存:dotnet nuget locals all --clear
    • 构建服务器同步清理缓存后,重新执行完整构建流程
  • 排查间接依赖:使用dotnet list package --include-transitive命令遍历所有项目的依赖树,定位到引用2.0.0版本的第三方包,要么升级该包到兼容.NET 7的版本,要么通过PackageReference的ExcludeAssets配置排除旧版本依赖。
  • 添加程序集绑定重定向:如果无法彻底统一版本,在应用程序的app.config或web.config中添加绑定重定向配置,强制CLR将旧版本请求导向7.0.0版本:
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" 
                          publicKeyToken="adb9793829ddae60" 
                          culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" 
                         newVersion="7.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 23:14:51