无法加载文件或程序集“System.Security.Cryptography.Algorithms,版本 = 4.1.0.0 [英] Could not load file or assembly 'System.Security.Cryptography.Algorithms, Version = 4.1.0.0

查看:26
本文介绍了无法加载文件或程序集“System.Security.Cryptography.Algorithms,版本 = 4.1.0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use System.Security.Cryptography.RNGCryptoServiceProvider class in my .NET Standard 1.4 library and according to this topic my code looks like this:

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

            return number;
        }
    }

我也从 NuGet 库安装:

I have also installed from NuGet libraries:

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

但是尝试启动它给了我:

But trying fire it up gives me:

'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 安装此版本后,我得到完全相同的异常.

And on NuGet page there is no 4.1.0.0 version, only 4.1.0-rc2-24027 and after installing this version I get exact same exception.

怎么了?

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

Edit: Switching from .NET Standard 1.4 to 1.6 didn't help

Edit2:

当我在 RandomNumberGenerator 上按 F12 时:

When I hit F12 on RandomNumberGenerator:

#region Assembly System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:Usersx.y.nugetpackagessystem.security.cryptography.algorithms4.3.0
ef
etstandard1.4System.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

So it wants 4.1.0 version (that not exists on NuGet) but path is set for 4.3.0

推荐答案

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

In addition to having a .NET Standard library you also have an application (like a console application) or perhaps a test project. The platform for the application determines what specific assembly referenced by your .NET Standard library to load.

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

So your library references System.Security.Cryptography.Algorithms 4.3.0 however the actual version of the assembly to load for your platform may be 4.1.0 (that is the version you get on .NET Framework 4.6.1).

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

So you need to inform your application to redirect the desired version (4.3.0) to the actual version for your runtime (4.1.0). You can do that in the app.config file. Remember that this file is used by the application and not the library. Adding an app.config file to your library will not make a difference.

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

I tried to create a small project like the one you describe that in addition to a .NET Standard 1.4 library that references System.Security.Cryptography.Algorithms 4.3.0 has a NET Framework 4.62 console application and I had to include an app.config file with the following contents for this to work:

<?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,这似乎不是什么问题.

Anecdotally, this seems to be less of a problem if you switch to .NET Standard 2.0.

这篇关于无法加载文件或程序集“System.Security.Cryptography.Algorithms,版本 = 4.1.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆