c# - 无法加载文件或程序集“System.Security.Cryptography.Algorithms,版本 = 4.1.0.0

标签 c# .net .net-standard .net-standard-1.4

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

    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

但尝试启动它会给我:

'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 page没有 4.1.0.0 版本,只有 4.1.0-rc2-24027安装此版本后,我得到完全相同的异常。

怎么了?

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

编辑2:

当我在 RandomNumberGenerator 上按 F12 时:

#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\ref\netstandard1.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 库之外,您还拥有一个应用程序(如控制台应用程序)或可能是一个测试项目。应用程序的平台决定了要加载的 .NET Standard 库引用的特定程序集。

因此您的图书馆引用 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 的 .NET Standard 1.4 库之外,我还尝试创建一个像您描述的那样的小项目。 4.3.0 有一个 NET Framework 4.62 控制台应用程序,我必须包含一个 app.config包含以下内容的文件才能正常工作:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
  </startup>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.1.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Security.Cryptography.Algorithms" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

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

关于c# - 无法加载文件或程序集“System.Security.Cryptography.Algorithms,版本 = 4.1.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46051831/

相关文章:

c# - 更改后面的 WPF 页面代码的基类

c# - 如何下载asp.net文件中的json对象

c# - JSON 计数只返回 1 而不是 2

c# - 如何在 Linq to SQL 中使用 distinct 和 group by

c# - 带有 DescriptionAttribute 的 .NET Standard 可移植库错误

c# - 如何将 Ninject 和 MVC 与单个接口(interface)的多个实现一起使用?

.net - 在 .Net 中阅读和编辑 HTML

.net-core - 如何在 SkiaSharp 中将字节数组转换为 SKBitmap?

c# - 将 PowerShell 包添加到 .netstandard2.0 类库

c# - 如何通过压缩纹理来节省尽可能多的内存