SignalR not working on iOS

Juan Pablo De Alba Venegas 0 Reputation points
2023-05-03T19:12:31.55+00:00

Hello community!

I'm using SignalR to send event notifications like when a player sends a friend request to another player or sends a Battle Request and everything works fine on the Unity Editor but when I make a build for iOS I get an error.

This is the code I'm using to initialize the SignalR Hub Connection

 public void BuildHubConnector() {
     string url = "verified_url_in_web_app_azure_services";
     var hubConnectionBuilder = new HubConnectionBuilder();
     hubConnectionBuilder.WithUrl(url);
     hubConnection = hubConnectionBuilder.Build();
 }

But when I try to run it on iOS, this error message appears.

 InvalidOperationException: Sequence contains no matching element
    
 System.Linq.Enumerable.Single[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) (at <00000000000000000000000000000000>:0)
 Microsoft.AspNetCore.SignalR.Client.HubConnection..cctor () (at <00000000000000000000000000000000>:0)
 System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor (System.Type serviceType) (at <00000000000000000000000000000000>:0)
 System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope serviceProviderEngineScope) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService (System.IServiceProvider provider, System.Type serviceType) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T] (System.IServiceProvider provider) (at <00000000000000000000000000000000>:0)
 SignalRPersistentConnection.Update () (at <00000000000000000000000000000000>:0)
 Rethrow as TypeInitializationException: The type initializer for 'Microsoft.AspNetCore.SignalR.Client.HubConnection' threw an exception.
 System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor (System.Type serviceType) (at <00000000000000000000000000000000>:0)
 System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope serviceProviderEngineScope) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService (System.IServiceProvider provider, System.Type serviceType) (at <00000000000000000000000000000000>:0)
 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T] (System.IServiceProvider provider) (at <00000000000000000000000000000000>:0)
 SignalRPersistentConnection.Update () (at <00000000000000000000000000000000>:0)

The steps that I'm following to use SignalR with unity are these:

  1. Create a dotnet project with the Microsoft.AspNetCore.SignalR.Client library. (I've tried many versions)
  2. Publish the project using "dotnet publish -c Release -r win-x64 --self-contained true".
  3. Copy the Dlls to a Plugins folder located inside the Assets folder in the unity project.
  4. Make the iOS build with unity and then make the build and run in XCode directly on my iphone.

That's it, everything works until this line of code "hubConnection = hubConnectionBuilder.Build();"

I don't understand why this happens only on iOS and not on Unity Editor, supposedly it is using a .Net framework compatible with both systems.

I'll appreciate any comments, thoughts, and of course, solutions. Thanks!

Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
131 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Stephen Halter 0 Reputation points Microsoft Employee
    2023-05-15T18:52:25.29+00:00

    This looks to be a trimming issue caused by reflection. The line that's failing is using reflection to resolve HubConnection.SendStreamItems. This can probably be fixed by removing Microsoft.AspNetCore.SignalR.Client.Core from the list of trimmed assemblies.

    <linker>
    
      <!--Preserve an entire assembly-->
     <assembly fullname="Microsoft.AspNetCore.SignalR.Client.Core" preserve="all"/>
    
    </linker>
    

    Unity - Manual: Managed code stripping (unity3d.com)

    This might just be enough to get you to the next issue though. This looks to currently be a game of whac-a-mole. Here's the issue tracking the Unity experience on GitHub: Using SignalR core with Unity and IL2CPP (AOT) · Issue #12102 · dotnet/aspnetcore (github.com).

    0 comments No comments