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

Cat*_*tia 8 c# macos openssl httpclient .net-core

我正在使用.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;
}
Run Code Online (Sandbox Code Playgroud)

我创建了指定运行时的发布包(在我的例子中是osx10.12-x64).在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.
Run Code Online (Sandbox Code Playgroud)

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

这个链接(https://github.com/dotnet/cli/issues/5129)告诉我如何解决我的机器中的问题.

如何在没有dotnet的客户机器上执行此操作?

bar*_*njs 13

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. 您的客户从https://www.microsoft.com/net/core#macos遵循.NET Core for 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/
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果您已完成独立构建,则可以使用libcrypto.1.0.0.dylib和libssl.1.0.0.dylib并将它们复制到应用程序目录中.

    • 从技术上讲,它们需要与System.Security.Cryptography.Native.OpenSsl.dylib位于同一目录中.
    • 请注意这一点,因为您正在分发安全组件.您的本地副本将胜过系统安装副本,因此您需要在任何OpenSSL安全版本发布后重新发布.
  3. 您稍等一下.NET Core 2.0,因为OpenSSL不再是对macOS的主要依赖(https://github.com/dotnet/corefx/issues/9394).