Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to load DLL 'System.Security.Cryptography.Native.OpenSsl' when creating new HttpClient

I'm working with .net core and need to execute a http call to my server.
The code used is as follow :

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;
}

I create the publish package with the runtime specified (osx10.12-x64 in my case). When working on osx I have this error :

(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.

My publish folder contain the "System.Security.Cryptography.Native.OpenSsl.dylib"

This link (https://github.com/dotnet/cli/issues/5129) told me how to solve the problem in my machine.

How can I do this in a customer's machine which do not have dotnet?

like image 931
Catia Avatar asked May 01 '17 15:05

Catia


2 Answers

System.DllNotFoundException: Unable to load DLL 'System.Security.Cryptography.Native.OpenSsl': The specified module could not be found. almost always means "I can't find OpenSSL" (libcrypto.1.0.0.dylib / libssl.1.0.0.dylib).

There are three major workarounds.

  1. You have your customer follow the .NET Core for macOS prerequisites from https://www.microsoft.com/net/core#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. If you have done a standalone build you can take the libcrypto.1.0.0.dylib and libssl.1.0.0.dylib and copy them into your application directory.

    • Technically, they need to be in the same directory as System.Security.Cryptography.Native.OpenSsl.dylib.
    • Be careful with this, since you're distributing a security component. Your local copy will trump a system installation copy, so you'll need to republish after any OpenSSL security releases.
  3. You wait a little bit for .NET Core 2.0, because OpenSSL is no longer a primary dependency on macOS (https://github.com/dotnet/corefx/issues/9394).
like image 192
bartonjs Avatar answered Oct 15 '22 16:10

bartonjs


The solution that worked for me was the second proposed by @bartonjs.

I had to modify my libssl.dylib since she referenced libcrypto with an absolute path.

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
like image 3
Catia Avatar answered Oct 15 '22 16:10

Catia