System.IO.FileNotFoundException 未能加载文件或程序集

最近被“未能加载文件或程序集“System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1”这一异常搞得头大,bing了很多资料都无法解决,最终通过两天的摸索终于成功。现梳理下解决的过程供大家参考。

程序调试过程中出现异常:

System.IO.FileNotFoundException: '未能加载文件或程序集“System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。'

在这里插入图片描述

查看System.Runtime.CompilerServices.Unsafe版本为6.0.0,历史版本中没有4.0.4,因此尝试降级为4.0.0。
在这里插入图片描述

点击“Update”后降级失败:

Attempting to gather dependency information for package 'System.Runtime.CompilerServices.Unsafe.4.0.0' with respect to project 'Common', targeting '.NETFramework,Version=v4.7.2'
Gathering dependency information took 7.47 sec
Attempting to resolve dependencies for package 'System.Runtime.CompilerServices.Unsafe.4.0.0' with DependencyBehavior 'Lowest'
One or more unresolved package dependency constraints detected in the existing packages.config file. All dependency constraints must be resolved to add or update packages. If these packages are being updated this message may be ignored, if not the following error(s) may be blocking the current package operation: 'NPOI 2.7.1 constraint: SixLabors.ImageSharp (>= 2.1.8)', 'System.Runtime.CompilerServices.Unsafe 6.0.0'
Unable to resolve dependencies. 'System.Runtime.CompilerServices.Unsafe 4.0.0' is not compatible with 'System.Memory 4.5.4 constraint: System.Runtime.CompilerServices.Unsafe (>= 4.5.3)', 'System.Threading.Tasks.Extensions 4.5.2 constraint: System.Runtime.CompilerServices.Unsafe (>= 4.5.2)'.
Time Elapsed: 00:00:07.9932170
========== Finished ==========

根据上面提示可知,存在多个依赖的约束(constraint)要求System.Runtime.CompilerServices.Unsafe的版本不能为4.0.0;其中:

  • NPOI 2.7.1要求System.Runtime.CompilerServices.Unsafe 6.0.0;
  • System.Memory 4.5.4要求System.Runtime.CompilerServices.Unsafe (>= 4.5.3);
  • System.Threading.Tasks.Extensions 4.5.2要求System.Runtime.CompilerServices.Unsafe (>= 4.5.2)

当前System.Runtime.CompilerServices.Unsafe为6.0.0,因此可尝试降级为4.5.3,再看下引用的dll是否满足要求,这样只需降级NPOI即可。

降级NPOI为2.5.6版本(成功)。
在这里插入图片描述

降级System.Runtime.CompilerServices.Unsafe为4.5.3版本(失败)。
在这里插入图片描述

降级失败提示:

Could not install package 'SixLabors.Fonts 2.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.7.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
Time Elapsed: 00:00:04.1623348

查看SixLabors.Fonts 2.0.0依赖信息,发现目标平台不符合要求。
在这里插入图片描述

只好卸载它。
没有其它依赖引用SixLabors.Fonts,成功卸载。

降级System.Runtime.CompilerServices.Unsafe为4.5.3版本,成功。
在这里插入图片描述

查看项目引用中System.Runtime.CompilerServices.Unsafe的dll版本为4.0.4.1,满足要求!
在这里插入图片描述

重新启动程序调试,没抛异常,问题解决!

问题的原因?令人费解!

System.Runtime.CompilerServices.Unsafe的版本明明已经更新为最新6.0.0,编译启动都没问题,为何在运行调试的过程中突然抛出异常,要求加载低版本的依赖?

个人不负责任的猜测某依赖没有添加对System.Runtime.CompilerServices.Unsafe的约束,导致nuget安装的时候认为可以升级到最新版。直到运行调用System.Runtime.CompilerServices.Unsafe方法的时候,才发现版本不正确。

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值