aspnetcore: SignalR 6.x doesn't work with android il2cpp

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

(Building with Unity) SignalR 6.x (6.0.0, 6.0.1, 6.0.2) doesn’t work with android on IL2CPP. Works fine on windows il2cpp builds. Reverting to 5.x fixes the issue.

Expected Behavior

No response

Steps To Reproduce

  • Create a unity project
  • Add SignalR 6.x libraries
  • Add a script that creates the hub connection, like this: var connection = new HubConnectionBuilder().WithUrl("http://localhost:5014/game").Build();
  • Build with Il2CPP to android
  • Exception (see below) is thrown when calling .Build()

Exceptions (if any)

03-06 12:42:04.721: E/Unity(28182): Parameter name: obj
03-06 12:42:04.721: E/Unity(28182):   at System.Threading.Monitor.ReliableEnterTimeout (System.Object obj, System.Int32 timeout, System.Boolean& lockTaken) [0x00000] in <00000000000000000000000000000000>:0 
03-06 12:42:04.721: E/Unity(28182):   at Microsoft.Extensions.DependencyInjection.DependencyInjectionEventSource.ServiceProviderBuilt (Microsoft.Extensions.DependencyInjection.ServiceProvider provider) [0x00000] in <00000000000000000000000000000000>:0 
03-06 12:42:04.721: E/Unity(28182):   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor (System.Collections.Generic.ICollection`1[T] serviceDescriptors, Microsoft.Extensions.DependencyInjection.ServiceProviderOptions options) [0x00000] in <00000000000000000000000000000000>:0 
03-06 12:42:04.721: E/Unity(28182):   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider (Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.DependencyInjection.ServiceProviderOptions options) [0x00000] in <00000000000000000000000000000000>:0 
03-06 12:42:04.721: E/Unity(28182):   at Mi```

### .NET Version

_No response_

### Anything else?

_No response_

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 3
  • Comments: 17 (1 by maintainers)

Most upvoted comments

I use this workaround to bypass that error:

var connection =
    new HubConnection(
        new HttpConnectionFactory(
            new OptionsWrapper<HttpConnectionOptions>(new HttpConnectionOptions
            {
                Transports = WebSockets,
                SkipNegotiation = true,
            }),
            new NullLoggerFactory()),
        new NewtonsoftJsonHubProtocol(),
        new UriEndPoint(new Uri(url)),
        new DummyServiceProvider(),
        new NullLoggerFactory(),
        new DefaultRetryPolicy())
    {
        ServerTimeout = Timeout,
    };

await connection.StartAsync();

Also 2 additional classes are needed:

internal sealed class DummyServiceProvider : IServiceProvider
{
    public object GetService(Type serviceType) => throw new NotSupportedException();
}

internal sealed class DefaultRetryPolicy : IRetryPolicy
{
    private static readonly TimeSpan?[] RetryDelays = { Zero, FromSeconds(2), FromSeconds(10), FromSeconds(30), null, };

    public TimeSpan? NextRetryDelay(RetryContext retryContext) => RetryDelays[retryContext.PreviousRetryCount];
}

@Deusald, thank you for sending me your example, it helped me. So I’ve figured out what is different. Wanna suggest to all non-experienced in Unity (like me), that came here to this discussion, please take a look if you have link.xml file in your Unity project. I put this file in the same folder where I have all my dlls. Long story short - this file makes that your dlls during il2cpp (Intermediate Language To C++) convertion will be not clipped. Here is official documentation regarding that: https://docs.unity3d.com/Manual/ManagedCodeStripping.html My file for example looks like this:

<linker>
  <assembly fullname="Microsoft.AspNetCore.Connections.Abstractions" preserve="all"/>
  <assembly fullname="Microsoft.AspNetCore.Http.Connections.Client" preserve="all"/>
  <assembly fullname="Microsoft.AspNetCore.Http.Connections.Common" preserve="all"/>
  <assembly fullname="Microsoft.AspNetCore.SignalR.Client.Core" preserve="all"/>
  <assembly fullname="Microsoft.AspNetCore.SignalR.Client" preserve="all"/>
  <assembly fullname="Microsoft.AspNetCore.SignalR.Common" preserve="all"/>
  <assembly fullname="Microsoft.AspNetCore.SignalR.Protocols.Json" preserve="all"/>
  <assembly fullname="Microsoft.Bcl.AsyncInterfaces" preserve="all"/>
  <assembly fullname="Microsoft.Bcl.TimeProvider" preserve="all"/>
  <assembly fullname="Microsoft.Extensions.DependencyInjection.Abstractions" preserve="all"/>
  <assembly fullname="Microsoft.Extensions.DependencyInjection" preserve="all"/>
  <assembly fullname="Microsoft.Extensions.Features" preserve="all"/>
  <assembly fullname="Microsoft.Extensions.Logging.Abstractions" preserve="all"/>
  <assembly fullname="Microsoft.Extensions.Logging" preserve="all"/>
  <assembly fullname="Microsoft.Extensions.Options" preserve="all"/>
  <assembly fullname="Microsoft.Extensions.Primitives" preserve="all"/>
  <assembly fullname="System.Buffers" preserve="all"/>
  <assembly fullname="System.ComponentModel.Annotations" preserve="all"/>
  <assembly fullname="System.Diagnostics.DiagnosticSource" preserve="all"/>
  <assembly fullname="System.IO.Pipelines" preserve="all"/>
  <assembly fullname="System.Memory" preserve="all"/>
  <assembly fullname="System.Numerics.Vectors" preserve="all"/>
  <assembly fullname="System.Runtime.CompilerServices.Unsafe" preserve="all"/>
  <assembly fullname="System.Text.Encoding.CodePages" preserve="all"/>
  <assembly fullname="System.Text.Encodings.Web" preserve="all"/>
  <assembly fullname="System.Text.Json" preserve="all"/>
  <assembly fullname="System.Threading.Channels" preserve="all"/>
  <assembly fullname="System.Threading.Tasks.Extensions" preserve="all"/>
</linker>

Also be carefull and use two spaces on the beginning of the line instead of tab, because with tabs it doesn’t work, at least on my side.

@Marik0511 I pulled SignalR code from my project. SignalRTest.zip There is an example server and client in unity. I tested it by making Andoird IL2CPP build and everything worked correctly. I was able to connect to server on my pc, send and receive messages.

Just tested the preview version (7.x), and the exception still occours. Will be using 5.0.17 for our project, but would like to use the newer version. When I have time, I’ll try to debug this more.

This is my hub connection creation code on unity client:

_HubConnection = new HubConnectionBuilder()
                .WithUrl(url, options =>
                 {
                     options.AccessTokenProvider = () => Task.FromResult(JwtToken)!;
                     options.SkipNegotiation     = true;
                     options.Transports          = HttpTransportType.WebSockets;
                 }).ConfigureLogging(options =>
                 {
                     options.SetMinimumLevel(LogLevel.Information);
                     options.AddProvider(_LoggerProvider);
                 }).Build();

Hi. It’s 2024 now, I have the same issue. I just wondering, there is no chanse to use SignalR client on application built for Android via Unity?.. How could it be like that?.. Or may be there is some another workaround and I just didn’t find it. Kindly please, if there is some hidden technique to use latest version of SignalR in game/app built with Unity for Android platform - write it here. Thank you in advance. Workaround from @kolesnick doesn’t work on the latest versoin of Unity. Will try SignalR version 5.0.17. (nowadays the latest version is 8.0.4)

I’m using unity 2023.2.20f1 and signalR in version 8.0.3 building for Android with IL2CPP and everything is working fine for me.

@Marik0511 It’s been a while since I had this issue, but I think I solved it by using Newtonsoft.JSON instead of MessagePack, as maybe MessagePack was doing some reflection which IL2CPP doesn’t support.

EDIT: The workaround used to work on SignalR Core 6.0.922.41926