xamarin开发常见错误总结--the operation was canceled


1.1.1  操作被取消异常

1.1.1.1 概述

发生的异常信息如下图所示:

1.1.1.2 原因分析

多数是因为http请求超时造成的;

1.1.1.3 参考链接

https://stackoverflow.com/questions/53291880/a-task-was-canceled-xamarin-exception

Yes, oddly HttpClient throws a TaskCancelledException for request timeouts. Here's a good blog post on timeouts with HttpClient: https://www.thomaslevesque.com/2018/02/25/better-timeout-handling-with-httpclient/

HttpClient does have a Timeout property that you can set: https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?redirectedfrom=MSDN&view=netframework-4.7.2

To set the timeout for all requests that use that HttpClient:

HttpClient client = new HttpClient();

client.Timeout = TimeSpan.FromSeconds(200); // this is double the default

That said, it would be good to find out why your request is taking more than 100 seconds to complete. Bad network? Server overworked and responding slowly? There are many reasons why a web request might be slow.

1.1.1.4 解决方案

1.1.1.4.1        检查接口服务是否启动

多常见于开发时,忘记启动接口服务

1.1.1.4.2        检查接口超时响应的原因

1.1.1.5 总结

暂无