SignalR 永远不会使用 Xamarin Forms 在 iOS 上启动

发布于 01-16 10:35 字数 6028 浏览 1 评论 0 原文

我正在尝试在我的 Xamarin Forms 项目中使用 SignalR。 当我使用 Android(设备或模拟器)时,它工作得很好。 但在 iOS 上使用相同的代码,它会挂在 await hubConnection.StartAsync();

我使用的是 Visual Studio 2022。 XAML 热重载已禁用。 我尝试添加 System.Memory 和 System.Buffers IncludeAssets None 修复。

这是代码

public async Task StartObserving()
{
    Uri signalRUri = new Uri("a-valid-uri-string-here");
    string jwt = await SecureStorage.GetAsync(SecureStorageKeys.IdToken);

    HubConnection hubConnection = new HubConnectionBuilder()
        .WithUrl(
            signalRUri,
            options =>
            {
                options.AccessTokenProvider = () => Task.FromResult(jwt);
                options.WebSocketConfiguration = (clientWebSocketOptions) =>
                {
                    clientWebSocketOptions.RemoteCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
                    {
                        return true;
                    };
                };
                options.HttpMessageHandlerFactory = (httpMessageHandler) =>
                {
                    if (httpMessageHandler is HttpClientHandler clientHandler)
                    {
                        clientHandler.ServerCertificateCustomValidationCallback = (HttpRequestMessage httpRequestMessage, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
                        {
                            return true;
                        };
                    }

                    return httpMessageHandler;
                };
            })
        .ConfigureLogging(logging =>
        {
            logging.SetMinimumLevel(LogLevel.Trace);
            logging.AddConsole();
        })
        .Build();

    hubConnection.On<string>(
        "Send",
        OnMessageReceivedFromSignalR);

    await hubConnection.StartAsync();
}

这是 SignalR 日志

2022-03-23 13:21:01.453 MyApp.iOS[2581:54607] [40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.SignalR.Client.HubConnection[40] 正在注册客户端方法“发送”的处理程序。

2022-03-23 13:21:01.464 MyApp.iOS[2581:54607] [40m[37mtrce[39m[22m[49m: Microsoft.AspNetCore.SignalR.Client.HubConnection[42] 等待 StartAsyncInner 中的连接锁定 (/_/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs:247)。

2022-03-23 13:21:01.472 MyApp.iOS[2581:54607] [40m[37mtrce[39m[22m[49m: Microsoft.AspNetCore.SignalR.Client.HubConnection[80] HubConnection 正在尝试从 Disconnected 状态转换到 Connecting 状态。

2022-03-23 13:21:01.476 MyApp.iOS[2581:54607] [40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.SignalR.Client.HubConnection[41] 正在启动 HubConnection。

2022-03-23 13:21:01.486 MyApp.iOS[2581:54607] [40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[1] 正在启动 HttpConnection。

2022-03-23 13:21:01.503 MyApp.iOS[2581:54607] [40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[5] 跳过处置,连接已处置。

2022-03-23 13:21:01.506 MyApp.iOS[2581:54607] [40m[37mtrce[39m[22m[49m: Microsoft.AspNetCore.SignalR.Client.HubConnection[80] HubConnection 正在尝试从“正在连接”状态转换到“已断开连接”状态。

2022-03-23 13:21:01.506 MyApp.iOS[2581:54607] [40m[37mtrce[39m[22m[49m: Microsoft.AspNetCore.SignalR.Client.HubConnection[20] 在 StartAsyncInner (/_/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs:280) 中释放连接锁定。

我收到以下异常

System.MissingMethodException:找不到方法: Microsoft.AspNetCore.Http.Connections.NegotiationResponse Microsoft.AspNetCore.Http.Connections.NegotiateProtocol.ParseResponse(System.ReadOnlySpan1) 在 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].Start[TStateMachine] (TStateMachine&stateMachine) [0x0002c] 中 /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:471 在 Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync (System.Uri url、System.Net.Http.HttpClient httpClient、 Microsoft.Extensions.Logging.ILogger 记录器, System.Threading.CancellationToken取消令牌)[0x0003d]中 :0 于 Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync (System.Uri uri、System.Threading.CancellationToken 取消令牌) [0x00011] 在 :0 处 Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport (Microsoft.AspNetCore.Connections.TransferFormat 传输格式, System.Threading.CancellationToken取消令牌)[0x0017b]中 :0 于 Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore (Microsoft.AspNetCore.Connections.TransferFormat 传输格式, System.Threading.CancellationToken取消令牌)[0x00127]中 :0 于 System.Threading.Tasks.ForceAsyncAwaiter.GetResult()[0x0000c]中 :0 于 Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync (Microsoft.AspNetCore.Connections.TransferFormat 传输格式, System.Threading.CancellationToken取消令牌)[0x00091]中 :0 于 Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync (System.Net.EndPoint 端点、System.Threading.CancellationToken 取消令牌)[0x00114] 在:0
在 Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync (System.Net.EndPoint 端点、System.Threading.CancellationToken cancelledToken) [0x001bf] in :0
在 Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore (System.Threading.CancellationToken取消令牌)[0x000a9]中 <22c2b3ef3848439db8bf52b72cbcab20>:0 在 Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner (System.Threading.CancellationToken取消令牌)[0x001a3]中 <22c2b3ef3848439db8bf52b72cbcab20>:0 在 System.Threading.Tasks.ForceAsyncAwaiter.GetResult()[0x0000c]中 <22c2b3ef3848439db8bf52b72cbcab20>:0 在 Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync (System.Threading.CancellationToken取消令牌)[0x00091]中 <22c2b3ef3848439db8bf52b72cbcab20>:0 在 MyApp.StartObserving () [0x0027b] 在 C:\Dev\MobileApp\MyApp\MyApp\Services\CacheService.cs:279

I'm trying to use SignalR in my Xamarin Forms project.
It works perfectly when I'm using Android (device or simulator).
But using the same code on iOS, it hangs at await hubConnection.StartAsync();

I'm using Visual Studio 2022.
XAML hot reload is disabled.
I've tried adding the System.Memory and System.Buffers IncludeAssets None fix.

Here's the code

public async Task StartObserving()
{
    Uri signalRUri = new Uri("a-valid-uri-string-here");
    string jwt = await SecureStorage.GetAsync(SecureStorageKeys.IdToken);

    HubConnection hubConnection = new HubConnectionBuilder()
        .WithUrl(
            signalRUri,
            options =>
            {
                options.AccessTokenProvider = () => Task.FromResult(jwt);
                options.WebSocketConfiguration = (clientWebSocketOptions) =>
                {
                    clientWebSocketOptions.RemoteCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
                    {
                        return true;
                    };
                };
                options.HttpMessageHandlerFactory = (httpMessageHandler) =>
                {
                    if (httpMessageHandler is HttpClientHandler clientHandler)
                    {
                        clientHandler.ServerCertificateCustomValidationCallback = (HttpRequestMessage httpRequestMessage, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
                        {
                            return true;
                        };
                    }

                    return httpMessageHandler;
                };
            })
        .ConfigureLogging(logging =>
        {
            logging.SetMinimumLevel(LogLevel.Trace);
            logging.AddConsole();
        })
        .Build();

    hubConnection.On<string>(
        "Send",
        OnMessageReceivedFromSignalR);

    await hubConnection.StartAsync();
}

Here's the SignalR log

2022-03-23 13:21:01.453 MyApp.iOS[2581:54607]
[40m[37mdbug[39m[22m[49m:
Microsoft.AspNetCore.SignalR.Client.HubConnection[40]
Registering handler for client method 'Send'.

2022-03-23 13:21:01.464 MyApp.iOS[2581:54607]
[40m[37mtrce[39m[22m[49m:
Microsoft.AspNetCore.SignalR.Client.HubConnection[42]
Waiting on Connection Lock in StartAsyncInner (/_/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs:247).

2022-03-23 13:21:01.472 MyApp.iOS[2581:54607]
[40m[37mtrce[39m[22m[49m:
Microsoft.AspNetCore.SignalR.Client.HubConnection[80]
The HubConnection is attempting to transition from the Disconnected state to the Connecting state.

2022-03-23 13:21:01.476 MyApp.iOS[2581:54607]
[40m[37mdbug[39m[22m[49m:
Microsoft.AspNetCore.SignalR.Client.HubConnection[41]
Starting HubConnection.

2022-03-23 13:21:01.486 MyApp.iOS[2581:54607]
[40m[37mdbug[39m[22m[49m:
Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[1]
Starting HttpConnection.

2022-03-23 13:21:01.503 MyApp.iOS[2581:54607]
[40m[37mdbug[39m[22m[49m:
Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[5]
Skipping dispose, connection is already disposed.

2022-03-23 13:21:01.506 MyApp.iOS[2581:54607]
[40m[37mtrce[39m[22m[49m:
Microsoft.AspNetCore.SignalR.Client.HubConnection[80]
The HubConnection is attempting to transition from the Connecting state to the Disconnected state.

2022-03-23 13:21:01.506 MyApp.iOS[2581:54607]
[40m[37mtrce[39m[22m[49m:
Microsoft.AspNetCore.SignalR.Client.HubConnection[20]
Releasing Connection Lock in StartAsyncInner (/_/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs:280).

I'm getting the following Exception

System.MissingMethodException: Method not found:
Microsoft.AspNetCore.Http.Connections.NegotiationResponse
Microsoft.AspNetCore.Http.Connections.NegotiateProtocol.ParseResponse(System.ReadOnlySpan1)
at
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].Start[TStateMachine]
(TStateMachine& stateMachine) [0x0002c] in
/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:471
at
Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync
(System.Uri url, System.Net.Http.HttpClient httpClient,
Microsoft.Extensions.Logging.ILogger logger,
System.Threading.CancellationToken cancellationToken) [0x0003d] in
:0 at
Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync
(System.Uri uri, System.Threading.CancellationToken cancellationToken)
[0x00011] in :0 at
Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport
(Microsoft.AspNetCore.Connections.TransferFormat transferFormat,
System.Threading.CancellationToken cancellationToken) [0x0017b] in
:0 at
Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore
(Microsoft.AspNetCore.Connections.TransferFormat transferFormat,
System.Threading.CancellationToken cancellationToken) [0x00127] in
:0 at
System.Threading.Tasks.ForceAsyncAwaiter.GetResult () [0x0000c] in
:0 at
Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync
(Microsoft.AspNetCore.Connections.TransferFormat transferFormat,
System.Threading.CancellationToken cancellationToken) [0x00091] in
:0 at
Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync
(System.Net.EndPoint endPoint, System.Threading.CancellationToken
cancellationToken) [0x00114] in :0
at
Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync
(System.Net.EndPoint endPoint, System.Threading.CancellationToken
cancellationToken) [0x001bf] in :0
at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore
(System.Threading.CancellationToken cancellationToken) [0x000a9] in
<22c2b3ef3848439db8bf52b72cbcab20>:0 at
Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner
(System.Threading.CancellationToken cancellationToken) [0x001a3] in
<22c2b3ef3848439db8bf52b72cbcab20>:0 at
System.Threading.Tasks.ForceAsyncAwaiter.GetResult () [0x0000c] in
<22c2b3ef3848439db8bf52b72cbcab20>:0 at
Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync
(System.Threading.CancellationToken cancellationToken) [0x00091] in
<22c2b3ef3848439db8bf52b72cbcab20>:0 at MyApp.StartObserving ()
[0x0027b] in C:\Dev\MobileApp\MyApp\MyApp\Services\CacheService.cs:279

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2025-01-23 10:35:43

好吧,我只实施了一半的解决方案。

对我来说解决这个问题的方法是将其添加到两个常见项目和iOS项目中。
不仅仅是常见的项目。

<PackageReference Include="System.Buffers">
  <Version>4.5.1</Version>
  <IncludeAssets>none</IncludeAssets>      
</PackageReference>
<PackageReference Include="System.Memory">
  <Version>4.5.4</Version>
  <IncludeAssets>none</IncludeAssets>      
</PackageReference>

Well I had only half of the solution implemented.

What fixed it for me was adding this to BOTH the common AND iOS projects.
Not only the common project.

<PackageReference Include="System.Buffers">
  <Version>4.5.1</Version>
  <IncludeAssets>none</IncludeAssets>      
</PackageReference>
<PackageReference Include="System.Memory">
  <Version>4.5.4</Version>
  <IncludeAssets>none</IncludeAssets>      
</PackageReference>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文