关于c#:无法加载文件或程序集’System.Security.Cryptography.Algorithms,版本= 4.1.0.0

您所在的位置:网站首页 安装net46 关于c#:无法加载文件或程序集’System.Security.Cryptography.Algorithms,版本= 4.1.0.0

关于c#:无法加载文件或程序集’System.Security.Cryptography.Algorithms,版本= 4.1.0.0

2023-09-02 08:46| 来源: 网络整理| 查看: 265

我正在尝试在.NET Standard 1.4库中使用System.Security.Cryptography.RNGCryptoServiceProvider类,根据此主题,我的代码如下所示:

12345678910    private byte[] GenerateRandomNumber(int length)     {         using (var randomNumberGenerator = RandomNumberGenerator.Create())         {             var number = new byte[length];             randomNumberGenerator.GetBytes(number);             return number;         }     }

我还从NuGet库安装了

System.Security.Cryptography.Algorithms v = 4.3.0 System.Security.Cryptography.Primitives v = 4.3.0

但是尝试将其发射给我:

1'Could not load file or package' System.Security.Cryptography.Algorithms, Version = 4.1.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a 'or one of its dependencies. The specified file could not be found. '

并且在NuGet页面上没有4.1.0.0版本,只有4.1.0-rc2-24027,安装此版本后,我得到了完全相同的异常。

怎么了?

编辑: 从.NET Standard 1.4切换到1.6并没有帮助

Edit2:

当我在RandomNumberGenerator上按F12键时:

123456789101112131415161718#region Assembly System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // C:\\Users\\x.y\\.nuget\\packages\\system.security.cryptography.algorithms\\4.3.0\ ef\ etstandard1.4\\System.Security.Cryptography.Algorithms.dll #endregion namespace System.Security.Cryptography {     public abstract class RandomNumberGenerator : IDisposable     {         protected RandomNumberGenerator();         public static RandomNumberGenerator Create();         public void Dispose();         public abstract void GetBytes(byte[] data);         protected virtual void Dispose(bool disposing);     } }

因此它需要4.1.0版本(NuGet上不存在),但路径设置为4.3.0

相关讨论 它说它对.NET Standard 1.4具有以下依赖性,您是否还添加了这些引用? System.IO (>= 4.3.0),System.Runtime (>= 4.3.0),System.Security.Cryptography.Primitives (>= 4.3.0) 是的,我有System.IO 4.3.0,System.Runtime 4.3.0和System.Security.Cryptography.Primitives 4.3.0 我建议您使用Fusion Log Viewer来获取有关加载错误的详细信息。即使您的bin文件夹中的程序集版本兼容,也可能与所请求的版本不同。然后,您必须在app.config中添加绑定重定向,以将所需版本重定向到实际版本。 Fusion Log Viewer不会显示有关它的任何数据(可能是因为程序抛出System.IO.FileNotFoundException?),如果它用于绑定重定向,则我目前正在单元测试中运行此代码,因此我认为它不会在那里工作。 @ Carlos28:我可以确认使用.NET Core / Standard时,Fusion Log Viewer似乎并不总是报告程序集绑定。我不知道这是一个错误还是正在等待实现的功能。但是,Fusion日志确实很有用,所以我希望我们将来能够继续使用它。 您从哪个项目运行此库? .NET Core控制台应用程序,经典ASP.NET Web应用程序,经典测试项目,.NET Core测试项目……?

除了拥有.NET Standard库之外,您还拥有一个应用程序(例如控制台应用程序)或一个测试项目。应用程序的平台确定要加载.NET标准库引用的特定程序集。

因此您的库引用了System.Security.Cryptography.Algorithms 4.3.0,但是要为平台加载的程序集的实际版本可能是4.1.0(即您在.NET Framework 4.6.1上获得的版本)。

因此,您需要通知您的应用程序将所需版本(4.3.0)重定向到运行时(4.1.0)的实际版本。您可以在app.config文件中执行此操作。请记住,该文件由应用程序而非库使用。将app.config文件添加到库中不会有任何影响。

我试图创建一个像您描述的项目一样的小项目,除了引用System.Security.Cryptography.Algorithms 4.3.0的.NET Standard 1.4库之外,还有一个NET Framework 4.62控制台应用程序,我必须包含一个app.config文件具有以下内容才能工作:

12345678910111213141516171819                                                                            

有趣的是,如果您切换到.NET Standard 2.0,这似乎没什么问题。

相关讨论 @ Carlos28:要解决您的问题,您需要确保System.Security.Cryptography.Algorithms.dll在bin文件夹中。如果程序集的版本与4.3.0不同,则必须创建绑定重定向以将4.3.0重定向到bin文件夹中的版本。您可能必须使用与答案中提供的重定向不同的重定向。这取决于您的应用程序的平台。

如果要在"经典"项目中使用此库,则可能需要在使用项目/库中激活自动绑定重定向生成(此处将单元测试项目计为库)。可以通过将这些属性添加到consumption(!)项目的csproj文件的属性中来完成:

1234   true   true

请参阅相关的"带有.NET Framework的.NET Standard 2.0的问题

相关讨论 这对我有用(Net 4.8引用Net Std 2.0)。我还已将 PackageReference 添加到同一属性组中。

n

我发现的解决方案:

将库从.NET Standard 1.4更新到2.0,然后可以与:

System.Security.Cryptography.Algorithms v = 4.3.0

System.Security.Cryptography.Primitives v = 4.3.0

根据本主题,它应在.NET Standard 1.3和以下版本上运行:

System.Security.Cryptography.Algorithms v = 4.2.0

System.Security.Cryptography.Primitives v = 4.0.0

由Martin Liversage提出的.Net Standard 1.4解决方案是不错的选择。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3