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.