Open michalpypek opened 2 years ago
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
Same here. Using Unity 2021.3, targeting netstandard2.1, Android IL2CPP. Version 6.x gives this exact exception. Running on the editor works fine. Have all the DLLs and link.xml necessary to prevent stripping.
Version 5.x works fine.
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.
I am experiencing this issue as well, any assistance will be greatly appreciated. In the meanwhile, it seems rolling back to 5x is the only solution.
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];
}
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)
@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
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.
@Deusald cool, and what kind of creation of HubConnection instance do you use?
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();
@Deusald, Thank you for answering me. I just copied your code, paste into newly created project in 2023.2.16f1. I use Windows11, Visual Studio 2022, all is updated. Also I have another project in another folder just for getting all dlls, this project is on netstandard 2.1, just like in Player Settings in Unity. I've published it in Release configuration and drag_n_drop all dlls into Unity's sub-folder. Here is how my project's file looks like:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.4" />
<PackageReference Include="SharpCompress" Version="0.37.2" />
</ItemGroup>
</Project>
The whole code in Unity project looks like this:
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using UnityEngine;
public class PresentationScript : MonoBehaviour
{
public TMPro.TextMeshProUGUI textMeshPro;
public void CreateConnectionButtonHandler()
{
try
{
var _HubConnection = new HubConnectionBuilder()
.WithUrl("https://myhub.com", options =>
{
options.AccessTokenProvider = () => Task.FromResult("some_token")!;
options.SkipNegotiation = true;
options.Transports = HttpTransportType.WebSockets;
}).ConfigureLogging(options =>
{
options.SetMinimumLevel(LogLevel.Information);
options.AddProvider(new MyLoggerProvider());
}).Build();
}
catch (Exception ex)
{
textMeshPro.text += ex.Message + "\n";
}
}
}
public class MyLoggerProvider : ILoggerProvider
{
public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName)
{
return new MyLogger();
}
public void Dispose()
{ }
}
public class MyLogger : Microsoft.Extensions.Logging.ILogger
{
public IDisposable BeginScope<TState>(TState state) where TState : notnull
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{ }
}
And the result on my Samsung S21 looks loke this: Error occurs on .Build(); action, only on Android. Don't have a clue what's the reason......
@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.
@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.
Is there an existing issue for this?
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
var connection = new HubConnectionBuilder().WithUrl("http://localhost:5014/game").Build();
.Build()
Exceptions (if any)