macOS 中的 dotnet 核心:"The type initializer for 'Crypto' threw an exception"

dotnet core in macOS: "The type initializer for 'Crypto' threw an exception"

我按照 .NET Core website 上的说明进行操作,但遇到了这个错误。显然缺少一些先决条件。知道如何安装这些吗?

mymac:~ naveen.vijay$ dotnet new

Unhandled Exception: System.TypeInitializationException: The type initializer for 'Crypto' threw an exception. ---> System.TypeInitializationException: The type initializer for 'CryptoInitializer' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'System.Security.Cryptography.Native': The specified module could not be found.
 (Exception from HRESULT: 0x8007007E)
   at Interop.CryptoInitializer.EnsureOpenSslInitialized()
   at Interop.CryptoInitializer..cctor()
   --- End of inner exception stack trace ---
   at Interop.Crypto..cctor()
   --- End of inner exception stack trace ---
   at Interop.Crypto.GetRandomBytes(Byte* buf, Int32 num)
   at System.IO.Path.GetCryptoRandomBytes(Byte* bytes, Int32 byteCount)
   at System.IO.Path.GetRandomFileName()
   at Microsoft.DotNet.InternalAbstractions.TemporaryDirectory..ctor()
   at Microsoft.Extensions.EnvironmentAbstractions.DirectoryWrapper.CreateTemporaryDirectory()
   at Microsoft.DotNet.Configurer.NuGetPackagesArchiver..ctor()
   at Microsoft.DotNet.Cli.Program.ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel)
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
   at Microsoft.DotNet.Cli.Program.Main(String[] args)
Abort trap: 6

macOS 的安装说明已过时。请参阅 Github 上的讨论:SSL Fails to Link Using Brew

我在 macOS El Capitan 上安装 .NET Core 时自己 运行 进入了这个。来自 的解决方案是 运行 如下:

sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib /usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.0.0/System.Security.Cryptography.Native.dylib

在我的终端中 运行 该命令后,dotnet new 工作正常。

我的 brew 安装发生了一些问题,我不得不 运行 以下命令来创建指向 OpenSSL 的符号链接(来自 Microsoft 上的 .Net Core 页面):

ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/ 
ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/ 

需要指定所需文件所在的位置来映射路径

sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib /usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.0.0/System.Security.Cryptography.Native.dylib

如果您对已安装的 dotnet 核心具有相同的文件夹结构,上述解决方案将有效。

/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.0.0/System.Security.Cryptography.Native.dylib

请检查文件 System.Security.Cryptography.Native.dylib 在您的系统中的位置,但是文件夹结构或包可能会根据更新发生变化。在我的例子中,路径是

/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.0.2/System.Security.Cryptography.Native.dylib