我正在使用Xamarin Forms创建一个个人项目。一段时间以来,我随机(概率为80%)得到以下异常,但我不知道为什么:“流的意外结束”。
堆栈跟踪的一部分:
---从上一个引发异常的位置开始的堆栈结束--- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task任务)[0x0003e]在 <43dbbdc147f2482093d8409abb04c233>:0在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task任务)在[0x00028]中 <43dbbdc147f2482093d8409abb04c233>:0在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task任务)在[0x00008]中 <43dbbdc147f2482093d8409abb04c233>:0在 System.Runtime.CompilerServices.TaskAwaiter
1[TResult].GetResult () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0 at System.IO.Stream.EndRead (System.IAsyncResult asyncResult) [0x0006a] in <43dbbdc147f2482093d8409abb04c233>:0 at System.IO.Stream+<>c.<BeginEndReadAsync>b__43_1 (System.IO.Stream stream, System.IAsyncResult asyncResult) [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0 at System.Threading.Tasks.TaskFactory
1 + FromAsyncTrimPromise1[TResult,TInstance].Complete (TInstance thisRef, System.Func
3 [T1,T2,TResult] endMethod, System.IAsyncResult asyncResult,System.Boolean requireSynchronization)[0x00000]在 <43dbbdc147f2482093d8409abb04c233>:0
这并不能告诉我很多。
这是引发异常的代码:
public async Task<string> PostDataToService(string sendURI, string json)
{
HttpClient client = Client;
string strresult = "";
try
{
Debug.WriteLine("### DataService#PostDataToService Posting: " + sendURI);
Debug.WriteLine("### JSON: " + json);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
_httpResponse = await client.PostAsync(sendURI, content);
if (_httpResponse.IsSuccessStatusCode)
{
strresult = await _httpResponse.Content.ReadAsStringAsync();
}
}
catch (Exception ex) when (ex is TaskCanceledException || ex is OperationCanceledException)
{
Debug.WriteLine("### DataService#PostDataToService Timeout!");
MessagingCenter.Send<App>((App)Application.Current, Settings.MESSAGING_COMMUNICATIONTIMEOUT);
}
catch (System.Exception ex)
{
Debug.WriteLine("### DataService#PostDataToService Exception@" + DateTime.Now);
Debug.WriteLine("### DataService#PostDataToService Error: " + ex.Message);
Debug.WriteLine(ex.StackTrace);
MessagingCenter.Send<App>((App)Application.Current, Settings.MESSAGING_COMMUNICATIONERROR);
}
Debug.WriteLine("### DataService#PostDataToService Returning for URI " + sendURI);
Debug.WriteLine("### DataService#PostDataToService Returning: " + strresult);
return strresult;
}
PostAsync方法中引发了实际的异常。
任何帮助都将不胜感激,因为我目前在几天之内要花几个小时。
编辑:一个附加信息:该帖子正在执行中,服务器已经在其中答复并返回了结果。