Car*_*s28 5 .net c# .net-standard .net-standard-1.4
我正在尝试在我的.NET Standard 1.4库中使用System.Security.Cryptography.RNGCryptoServiceProvider类,根据这个主题,我的代码如下所示:
private byte[] GenerateRandomNumber(int length)
{
using (var randomNumberGenerator = RandomNumberGenerator.Create())
{
var number = new byte[length];
randomNumberGenerator.GetBytes(number);
return number;
}
}
Run Code Online (Sandbox Code Playgroud)
我也从NuGet库安装:
但尝试解雇它会让我:
'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. '
Run Code Online (Sandbox Code Playgroud)
在NuGet页面上没有4.1.0.0版本,只有4.1.0-rc2-24027,安装此版本后我得到完全相同的异常.
怎么了?
编辑:从.NET标准1.4切换到1.6没有帮助
编辑2:
当我点击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);
}
}
Run Code Online (Sandbox Code Playgroud)
所以它想要4.1.0版本(NuGet上不存在)但路径设置为4.3.0
除了拥有.NET标准库,您还可以使用应用程序(如控制台应用程序)或测试项目.应用程序的平台确定要加载的.NET标准库引用的特定程序集.
因此,您的库引用了System.Security.Cryptography.Algorithms4.3.0,但是要为您的平台加载的程序集的实际版本可能是4.1.0(这是您在.NET Framework 4.6.1上获得的版本).
因此,您需要通知应用程序将所需版本(4.3.0)重定向到运行时的实际版本(4.1.0).你可以在app.config文件中做到这一点.请记住,该文件由应用程序使用,而不是库.将app.config文件添加到库中不会产生任何影响.
我尝试创建一个像你描述的那个小项目,除了引用System.Security.Cryptography.Algorithms4.3.0 的.NET Standard 1.4库之外还有一个.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>
Run Code Online (Sandbox Code Playgroud)
有趣的是,如果你切换到.NET Standard 2.0,这似乎不是一个问题.
如果这个库要在“经典”项目中使用,你可能需要在消费项目/库中激活自动绑定重定向生成(单元测试项目在这里算作库)。这可以通过将这些添加到消耗(!)项目的 csproj 文件的属性来完成:
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息和选项,请参阅相关的“带有 .NET Framework 和 NuGet 的 .NET Standard 2.0 的问题”公告帖子。
| 归档时间: |
|
| 查看次数: |
8978 次 |
| 最近记录: |