runtime: Cannot find `libSystem.Security.Cryptography.Native.OpenSsl` assembly when using Roslyn in Blazor WASM

Describe the bug

I am using Roslyn (e.g. Microsoft.CodeAnalysis.CSharp.*) in a Blazor WASM project. My code was working fine in 3.2, but after upgrading to 5.0-Preview7, I now receive the following runtime error when calling SemanticModel.GetDiagnostics().

blazor.webassembly.js:1 Uncaught (in promise) Error: System.TypeInitializationException: The type initializer for 'Crypto' threw an exception.
 ---> System.DllNotFoundException: libSystem.Security.Cryptography.Native.OpenSsl assembly:<unknown assembly> type:<unknown type> member:(null)
   at Interop.Crypto..cctor()
   --- End of inner exception stack trace ---
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
--- End of stack trace from previous location ---
   at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.InvokeSynchronously(JSRuntime jsRuntime, DotNetInvocationInfo& callInfo, IDotNetObjectReference objectReference, String argsJson)
   at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo invocationInfo, String argsJson)
    at Object.endInvokeDotNetFromJS (http://localhost:51214/_framework/blazor.webassembly.js:1:3112)
    at Object.invokeJSFromDotNet (http://localhost:51214/_framework/blazor.webassembly.js:1:2732)
    at _mono_wasm_invoke_js_marshalled (http://localhost:51214/_framework/dotnet.5.0.0-preview.7.20364.11.js:1:164798)
    at do_icall (<anonymous>:wasm-function[5230]:0xe660d)
    at do_icall_wrapper (<anonymous>:wasm-function[1665]:0x4787d)
    at interp_exec_method (<anonymous>:wasm-function[1200]:0x29604)
    at interp_runtime_invoke (<anonymous>:wasm-function[4814]:0xcc5f0)
    at mono_jit_runtime_invoke (<anonymous>:wasm-function[4317]:0xb63e1)
    at do_runtime_invoke (<anonymous>:wasm-function[1664]:0x477c6)
    at mono_runtime_try_invoke (<anonymous>:wasm-function[422]:0xd74c)

To Reproduce

In a Blazor WASM .Net 5.0-Preview7 project, add references to latest C# Roslyn compiler assemblies, and invoke the Roslyn compiler as follows:

var compilation = CSharpCompilation.Create(...);
var model = compilation.GetSemanticModel(tree);
var diag = model.GetDiagnostics();  // <--  Exception thrown here

Code works fine in 3.2, but fails in 5.0-Preview7.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 21 (16 by maintainers)

Most upvoted comments

This also leads to signalr being unable to connect via websockets to a remote server in blazor wasm:

fail: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[11]
      Failed to start connection. Error starting transport 'WebSockets'.
System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server
 ---> System.TypeInitializationException: The type initializer for 'Crypto' threw an exception.
 ---> System.DllNotFoundException: libSystem.Security.Cryptography.Native.OpenSsl assembly:<unknown assembly> type:<unknown type> member:(null)
   at Interop.Crypto..cctor()
   --- End of inner exception stack trace ---
   at Internal.Cryptography.HashProviderDispenser.CreateHashProvider(String hashAlgorithmId)
   at System.Security.Cryptography.SHA1.Implementation..ctor()
   at System.Security.Cryptography.SHA1.Create()
   at System.Net.WebSockets.WebSocketHandle.CreateSecKeyAndSecWebSocketAccept()
   at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore(Uri uri, CancellationToken cancellationToken, ClientWebSocketOptions options)
   at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore(Uri uri, CancellationToken cancellationToken, ClientWebSocketOptions options)
   at System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore(Uri uri, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport.StartAsync(Uri url, TransferFormat transferFormat, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartTransport(Uri connectUrl, HttpTransportType transportType, TransferFormat transferFormat, CancellationToken cancellationToken)