Xamarin.ios 应用在 HttpClient 异步请求上崩溃(SIGABRT:套接字未连接)



我有一个问题,我在异步任务中使用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)

不确定我是否应该做一些东西。 谢谢!

您只需要添加空检查,因为请求超时。

它不侦听响应,因此返回 null。你可以做这样的事情:

var content = await response.Content.ReadAsStreamAsync();
if (content != null)
{
using (var streamReader = new StreamReader(content))
{
var content_json = streamReader.ReadToEnd();
if (content_json != null)
txtbox1.Text = content_json;
}
}

你的崩溃应该消失了。

相关内容

最新更新