我想在我的Xamarin表单项目中使用SignalR.

我正在使用VisualStudio2022.

这是密码

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();
}

这是信号员日志(log)

2022-03-23 13:21:01.453我的apply.iOS[2581:54607]

2022-03-23 13:21:01.464我的apply.iOS[2581:54607]

2022-03-23 13:21:01.472我的apply.iOS[2581:54607]

2022-03-23 13:21:01.476我的应用程序.iOS[2581:54607]

2022-03-23 13:21:01.486我的应用程序.iOS[2581:54607]

2022-03-23 13:21:01.503我的apply.iOS[2581:54607]

2022-03-23 13:21:01.506我的apply.iOS[2581:54607]

2022-03-23 13:21:01.506我的apply.iOS[2581:54607]

我得到了以下例外

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

推荐答案

我只实施了一半的解决方案.

对我来说,解决这个问题的方法是将它添加到BOTH个常见和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>

Ios相关问答推荐

如何在进行滑动删除操作时保持自定义视图背景静态

自定义alert 未执行任何操作

验证开发人员应用程序证书是否已在您的设备上验证

在相机动作之前快速防止场景视点重置

SwiftUI检测顶级和安全区域插图

如何在KMM项目中处理PHPickerViewController的回调?

许多文本字段的性能问题

为什么在Actor内部使用withTaskGroupwork并行运行?

在自定义 ButtonStyle 中修改形状 colored颜色

ITMS-90432:将应用上传到应用store 时出现问题

SwiftUI 图像与下拉视图切换一起 skip

在 SwiftUI 中打开 PDF

用溢出的长文本对齐 Flutter 中的行和列

将视图(例如 Text())不透明度设置为 0 或将背景设置为 Color.clear 会导致它不被绘制

UIScrollview 获取touch 事件

Watchkit AppIcon - 名为AppIcon的应用图标集没有任何适用的内容

如何在导航栏右侧添加多个 UIBarButtonItem?

由于 NSInvalidUnarchiveOperationException 导致 iOS11 WKWebview 崩溃

故事板中的 colored颜色 与 UIColor 不匹配

如何在 iOS 中获取正在运行的应用程序的名称