The assembly for System.Buffers could not be loaded;无法加载System.Buffers的程序集

调用Redis类库: StackExchange/StackExchange.Redis  进行Redis连接时,有时会发生:

如下错误

英文:

The assembly for System.Buffers could not be loaded; this usually means a missing assembly binding redirect - try checking this, and adding any that are missing; note that it is not always possible to add this redirects - for example 'azure functions v1'; it looks like you may need to use 'azure functions v2' for that - sorry, but that's out of our control

中文:

无法加载System.Buffers的程序集;这通常意味着缺少程序集绑定重定向-请尝试检查此重定向,并添加任何缺少的重定向;请注意,并不总是可以添加此重定向-例如“Azure函数v1”;看起来您可能需要使用“Azure函数v2”进行重定向-抱歉,但这已超出您的权限。

原因分析:

参考链接:https://github.com/dotnet/corefx/issues/32511

Pipelines.Sockets.Unofficial 类库,依赖与:

System.IO.Pipelines (>= 4.5.1) 

System.Buffers (>= 4.4.0) 

 而System.IO.Pipelines(4.5.1)版本依赖于 

NETStandard.Library (>= 1.6.1)

System.Memory (>= 4.5.1)

System.Buffers (>= 4.4.0) 

System.Threading.Tasks.Extensions (>= 4.5.1)

 而System.IO.Pipelines(4.5.3)版本依赖于

 System.Memory (>= 4.5.1)

 System.Buffers (>= 4.4.0) 

如果系统里面引用的System.IO.Pipelines 和 System.Buffers 文件版本,低于要求: System.IO.Pipelines (>= 4.5.1)  、 System.Buffers (>= 4.4.0)  时,就会发生上述依赖报错问题。

解决方法:

1.查找所有项目引用,更新类库Pipelines.Sockets.Unofficial 到最新版本(当前是 v2.0.22)

2.查找所有项目引用,更新 System.IO.Pipelines 类库版本,版本至少要 >= 4.5.1,最好是 >= 4.5.2,这样只会引用 System.Memory (>= 4.5.1)、  System.Buffers (>= 4.4.0) 两个文件,而4.5.1版本需要引用4个文件

3.查找所有项目引用,更新 System.Buffers 类库版本,版本至少要 >= 4.4.0

如果连接时出现如下异常 :

未能加载文件或程序集“System.IO.Pipelines, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51”或它的某一个依赖项。系统找不到指定的文件。

说明bin目录下没有System.IO.Pipelines文件,此时可以在启动项目.exe项目下,添加对System.IO.Pipelines(版本至少要 >= 4.5.1)的引用,并设置为复制到输出目录

 如果引用的是 System.IO.Pipelines ( v4.5.1)  且引用了 System.Threading.Tasks.Extensions (版本低于4.5.2),也会报错,因为System.IO.Pipelines ( v4.5.1) 需要引用 System.Threading.Tasks.Extensions (>= 4.5.2)

参考链接:

https://github.com/dotnet/corefx/issues/32511

https://github.com/dotnet/corefx/issues/32457

原文地址:https://www.cnblogs.com/xytmj/p/11075510.html