2017-09-05 65 views
0

我想在我的.NET标准1.4库中使用System.Security.Cryptography.RNGCryptoServiceProvider类,并根据this主题my代码如下所示:无法加载文件或程序集'System.Security.Cryptography.Algorithms,版本= 4.1.0.0

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标准 切换1.4〜1.6没有帮助

EDIT2

当我打F12上RandomNumberGenerator

#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

+0

它说它具有.NET Standard 1.4的以下依赖关系,您是否也添加了这些引用? 'System.IO(> = 4.3.0)','System.Runtime(> = 4.3.0),'System.Security.Cryptography.Primitives(> = 4.3.0)' – Equalsk

+0

是的,我有System.IO 4.3 .0,System.Runtime 4.3.0和System.Security.Cryptography.Primitives 4.3.0 – Carlos28

+0

我建议你使用[Fusion Log Viewer](https://docs.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer)来获取有关加载错误的详细信息。尽管它是兼容的,但bin文件夹中的程序集版本很可能与请求的版本不同。然后你必须在你的'app.config'中添加一个[绑定重定向](https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions)重定向所需的版本到实际的版本。 –

回答

1

除了拥有.NET标准库之外,您还拥有一个应用程序(如控制台应用程序)或可能是一个测试项目。应用程序的平台决定了你的.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文件添加到您的库不会有什么区别。

我试图创建一个小项目,像你描述的,除了一个.NET标准1.4库引用System.Security.Cryptography.Algorithms 4.3.0具有.NET框架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标准2.0。

+0

不错的解决方案!但不幸的是,我由于某种原因仍然得到相同的例外... – Carlos28

+0

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

0

的解决方案,我发现:

  • 更新库从.NET标准1.4〜2。0然后它与:

  • System.Security.Cryptography.Algorithms V = 4,3,0

  • System.Security.Cryptography.Primitives V = 4,3,0

  • 根据this主题,它应该在.NET标准1.3和:

  • System.Security.Cryptography.Algorithms V = 4.2.0

  • System.Security.Cryptography.Primitives V = 4.0.0

对于.Net标准1.4解决方案Martin Liversage提出的是不错的选择。

2

如果此库用于“经典”项目,则可能需要在消耗项目/库(单元测试项目在此计数为库)中激活自动绑定重定向生成。 (!),这可以通过将这些以性能为消费的的csproj文件来完成项目:

<PropertyGroup> 
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> 
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> 
</PropertyGroup> 

更多详细信息和选项请参阅相关"Issues with .NET Standard 2.0 with .NET Framework & NuGet" announcement post

相关问题