我们发现这AutoGenerateBindingRedirects可能是导致此问题的原因。
观察到:以相同的项目为目标,net45并且netstandard1.5成功在一台计算机上构建,而未能在另一台计算机上构建。机器安装了不同版本的框架(4.6.1-成功和4.7.1-故障)。将第一台计算机上的框架升级到4.7.1后,构建也失败了。
Error Message:
System.IO.FileNotFoundException : Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
----> System.IO.FileNotFoundException : Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Auto binding redirects是的功能.net 4.5.1。每当nuget检测到项目正在传递引用同一程序集的不同版本时,它将在输出目录中自动生成配置文件,将所有版本重定向到所需的最高版本。
在我们的情况下,它会将的所有版本重新绑定System.Runtime到Version=4.1.0.0。.net 4.7.1附带一个4.3.0.0版本的运行时。因此,重定向绑定被映射到当前版本的框架中不可用的版本。
该问题已通过禁用4.5目标的自动绑定重定向而仅将其保留为.net核心来解决。
<PropertyGroup Condition="'$(TargetFramework)' == 'net45'">
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>