2017-09-05 221 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

相關問題