Xamarin.ios应用程序因HttpClient异步请求而崩溃(SIGABRT:套接字未连接)

时间:2019-10-04 04:14:06

标签: xamarin websocket xamarin.ios httpclient

我有一个问题,我正在使用HttpClient在异步任务中从API提取大型JSON内容。当应用程序在后台执行请求时锁定设备屏幕时,会发生问题。如果我再次打开该应用程序,它将崩溃(SIGABRT:该套接字未连接)

这是github上的项目: https://github.com/aproram/httpclient-bug

为了复制问题,请按照以下步骤精确复制问题: -确保应用已终止且未在后台运行 -打开应用 -按下主屏幕按钮。 锁电话 -解锁电话 -再次打开该应用程序,它将崩溃。

崩溃堆栈跟踪(使用AppHockey):

Socket.EndReceive (System.IAsyncResult asyncResult)
NetworkStream.EndRead (System.IAsyncResult asyncResult)

NetworkStream.EndRead (System.IAsyncResult asyncResult)
Stream+<>c.<BeginEndReadAsync>b__45_1 (System.IO.Stream stream, System.IAsyncResult asyncResult)
TaskFactory`1+FromAsyncTrimPromise`1[TResult,TInstance].Complete (TInstance thisRef, System.Func`3[T1,T2,TResult] endMethod, System.IAsyncResult asyncResult, System.Boolean requiresSynchronization)
MobileAuthenticatedStream.InnerRead (System.Boolean sync, System.Int32 requestedSize, System.Threading.CancellationToken cancellationToken)
AsyncProtocolRequest.InnerRead (System.Threading.CancellationToken cancellationToken)
AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken)
AsyncProtocolRequest.StartOperation (System.Threading.CancellationToken cancellationToken)
MobileAuthenticatedStream.StartOperation (Mono.Net.Security.MobileAuthenticatedStream+OperationType type, Mono.Net.Security.AsyncProtocolRequest asyncRequest, System.Threading.CancellationToken cancellationToken)
BufferedReadStream.ProcessReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Threading.CancellationToken cancellationToken)
WebReadStream.ReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Threading.CancellationToken cancellationToken)
FixedSizeReadStream.ProcessReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Threading.CancellationToken cancellationToken)
WebReadStream.ReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Threading.CancellationToken cancellationToken)

HttpWebRequest.RunWithTimeoutWorker[T] (System.Threading.Tasks.Task`1[TResult] workerTask, System.Int32 timeout, System.Action abort, System.Func`1[TResult] aborted, System.Threading.CancellationTokenSource cts)
WebResponseStream.ReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancellationToken)
Stream.CopyToAsyncInternal (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken)
HttpContent.LoadIntoBufferAsync (System.Int64 maxBufferSize)
HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken)
ViewController.FetchtheJSON ()
AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state)
NSAsyncSynchronizationContextDispatcher.Apply ()
(wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate)
UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName)
Application.Main (System.String[] args)

不确定我应该做些什么。 谢谢!

1 个答案:

答案 0 :(得分:0)

由于请求超时,您只需要添加空检查。

它不监听响应,因此返回null。您可以执行以下操作:

          Date sulfate nitrate ID
1   2001-01-01      NA      NA  2
2   2001-01-02      NA      NA  2
3   2001-01-03      NA      NA  2
4   2001-01-04      NA      NA  2
5   2001-01-05      NA      NA  2
6   2001-01-06      NA      NA  2
7   2001-01-07      NA      NA  2
8   2001-01-08      NA      NA  2
9   2001-01-09      NA      NA  2
10  2001-01-10      NA      NA  2
11  2001-01-11      NA      NA  2
12  2001-01-12      NA      NA  2
13  2001-01-13      NA      NA  2
14  2001-01-14      NA      NA  2
15  2001-01-15      NA      NA  2
16  2001-01-16      NA      NA  2
17  2001-01-17      NA      NA  2
18  2001-01-18      NA      NA  2
19  2001-01-19    2.30   0.699  2
20  2001-01-20      NA      NA  2
21  2001-01-21      NA      NA  2
22  2001-01-22      NA      NA  2
23  2001-01-23      NA      NA  2
24  2001-01-24      NA      NA  2
25  2001-01-25    2.19   4.970  2

您的崩溃应该消失了。