1

I am using signalR client in unity for a real-time Android game.
So, when i run game in Unity editor, everything is fine, But when i built it for android, i realize that it didn't connect to Hub server.
Then i used adb logcat to debug and find bug.
So i realize that, there is an exception exist in the following codes, exactly in the build function:

         connection = new HubConnectionBuilder()
                 .WithUrl(ApiNeed.UrlNeed + "/game", opts =>
                 {
                     opts.HttpMessageHandlerFactory = (message) =>
                     {
                         if (message is HttpClientHandler clientHandler)
                             // always verify the SSL certificate
                             clientHandler.ServerCertificateCustomValidationCallback +=
                                 (sender, certificate, chain, sslPolicyErrors) => { return true; };
                         return message;
                     };
                     opts.CloseTimeout = new TimeSpan(0, 5, 0);
                     if (Token.ToLower().StartsWith("bearer"))
                     {
                         Token = Token.Substring("bearer".Length);
                     }
                     opts.Headers.Add("Authorization", Token);
                 }
                 )
                 .Build(); // Problem is here 

Then i looked for exception, so i put that code in try catch block. Then i logged the exception message, stacktrace and source. The result is below:

The type initializer for 'Microsoft.AspNetCore.SignalR.Client.HubConnection' threw an 
exception.myQm:   at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object 
obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00000] in 
<00000000000000000000000000000000>:0 myQm:   at 
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstruct
or (Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, 
Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) [0x00000] in
 <00000000000000000000000000000000>:0 myQm:   at Microsoft.Extensions.DependencyInjection.ServiceLookup
.CallSiteVisitor2[TArgument,TResult].VisitCallSiteMain (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite 
callSite, TArgument argument) [0x00000] in   
<00000000000000000000000000000000>

Edited 7/22/2024
I tried to build it with Mono backend script, it works fine in Mono, but in mono scripting backend, the problem is that i can't build for x64 cpu architecture, also google warn that app is insecure before installing APK. Furthermore i need to check ARM64 that is disable when i use Mono as scripting backend. That's why i must to use IL2CPP scripting backend.

4
  • Please try to enable signalr client logging to check if we can find more details. Commented Jul 18, 2024 at 7:30
  • And I believe the logging may not helps in this issue, may I know the package version in your project ? Commented Jul 18, 2024 at 8:05
  • @JasonPan Hello, I'm using signalR.net core version 6. I downloaded DLLs with nuget from other .net core project, and put required Dlls in my unity project, also you were right, logging didn't help. Do you have any suggestions for me to do? Commented Jul 21, 2024 at 7:49
  • @JasonPan I added some more details that i found in last 2 days. please check question again. Commented Jul 21, 2024 at 22:17

2 Answers 2

3

Solve:
For anyone who might run into this problem, Finally, I found that IL2CPP removes classes that seem useless. Therefore, in the linker.xml file, I added the classes that I want not to be deleted. Therefore, the compiler does not remove them.
Just put link.xml in asset folder of your project. The compiler automatically detects it and applies it in the build process.

<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.Http.Features" 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.Extensions.Configuration.Abstractions" preserve="all"/> 
    <assembly fullname="Microsoft.Extensions.Configuration.Binder" preserve="all"/>
    <assembly fullname="Microsoft.Extensions.Configuration" preserve="all"/>
    <assembly fullname="Microsoft.Extensions.DependencyInjection.Abstractions" preserve="all"/>
    <assembly fullname="Microsoft.Extensions.DependencyInjection" 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.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.Json" preserve="all"/>
    <assembly fullname="System.Threading.Channels" preserve="all"/>
    <assembly fullname="System.Threading.Tasks.Extensions" preserve="all"/>
</linker>

To be sure, I added the services once again:

var con = new HubConnectionBuilder()
          .WithUrl("https://YOUR_HOST/game");
// i added services again to be sure that no problem occures anymore.
con.Services.AddSingleton<IHubConnectionBuilder, HubConnectionBuilder>();
con.Services.AddSingleton<IServiceProvider, ServiceProvider>();
con.Services.AddSingleton<ILoggerFactory, LoggerFactory>();
con.Services.AddSingleton<IHubConnectionBuilder, HubConnectionBuilder>();
con.Services.AddSingleton<IHubProtocol, JsonHubProtocol>();
connection = con.Build();
Sign up to request clarification or add additional context in comments.

1 Comment

You are savior :)
1

Thank you mohammadreza khorasani! I have another errors but your solution helps me too.

My exception:

InvalidOperationException: Sequence contains no matching element System.Linq.Enumerable.Single[TSource] (System.Collections.Generic.IEnumerable1[T] source, System.Func2[T,TResult] predicate) (at <00000000000000000000000000000000>:0) Microsoft.AspNetCore.SignalR.Client.HubConnection..cctor () (at <00000000000000000000000000000000>:0)...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.