无法加载DLL“System.Security.Cryptography.Native.OpenSsl”创建新的HttpClient时

问题描述 投票:7回答:3

我与.NET的核心工作,需要执行一个HTTP调用到我的服务器。 使用的代码如下:

try
{
    var client = new HttpClient ();
    var helloUri = new System.Uri (string.Concat ("http://localhost:1234", "/hello"));
    var response = client.GetAsync (helloUri);

    var helloReponse = response.Result;
    return helloReponse.StatusCode == System.Net.HttpStatusCode.OK;
}
catch (System.Exception e)
{
    throw e;
}

我创建的发布包与运行时指定的(在我的情况osx10.12-64)。在OSX工作时我有这样的错误:

(The type initializer for 'System.Net.Http.CurlHandler' threw an 
exception.) ---> 
System.TypeInitializationException: The type 
initializer for 'System.Net.Http.CurlHandler' threw an exception. ---> 
System.TypeInitializationException: The type initializer for 'Http' 
threw an exception. ---> 
System.TypeInitializationException: The type 
initializer for 'HttpInitializer' threw an exception. ---> 
System.TypeInitializationException: The type initializer for 
'CryptoInitializer' threw an exception. ---> 
System.DllNotFoundException: Unable to load DLL 
'System.Security.Cryptography.Native.OpenSsl': The specified module 
could not be found.

我的发布文件夹中包含“System.Security.Cryptography.Native.OpenSsl.dylib”

此链接(https://github.com/dotnet/cli/issues/5129)告诉我怎么解决这个问题在我的机器。

我怎样才能做到这一点在客户机不具备的dotnet?

c# macos openssl httpclient .net-core
3个回答
11
投票

System.DllNotFoundException: Unable to load DLL 'System.Security.Cryptography.Native.OpenSsl': The specified module could not be found.几乎总是意味着 “我找不到OpenSSL的”(libcrypto.1.0.0.dylib / libssl.1.0.0.dylib)。

主要有三个解决方法。

  1. 你有你的客户按照.NET的核心从https://www.microsoft.com/net/core#macos MacOS的先决条件: $ brew update $ brew install openssl $ mkdir -p /usr/local/lib $ 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/
  2. 如果你做了一个独立的构建可以采取libcrypto.1.0.0.dylib和libssl.1.0.0.dylib,并将它们复制到你的应用程序目录。 从技术上来说,他们需要在同一目录System.Security.Cryptography.Native.OpenSsl.dylib。 要小心,因为你分发安全组件。您的本地副本将王牌系统安装拷贝,所以你需要任何OpenSSL的安全发布后重新发布。
  3. 你等一点点.NET 2.0的核心,因为OpenSSL是不再在MacOS(https://github.com/dotnet/corefx/issues/9394)的主要依赖。

3
投票

为我工作的解决方案是第二个由@bartonjs建议。

我不得不修改我的libssl.dylib因为她引用libcrypto绝对路径。

otool -l libssl.1.0.0.dylib 
showed the absolute path

install_name_tool -change usr/../Cellar/.. @rpath/libcrypto.1.0.0.dylib libssl.1.0.0.dylib
to change the path

0
投票

是的,我使用网络核心2.2,而不是使用NET内核1.1.2解决了这个问题

您可以下载并安装由本

https://github.com/dotnet/core/blob/master/release-notes/2.2/2.2.1/2.2.1-download.md

© www.soinside.com 2019 - 2024. All rights reserved.