user10200885
user10200885

Reputation:

A task was canceled Xamarin Exception

I am getting a "A task was canceled exception" in my PostAsync. What is the cause of this? How can I fix this? I research the possible cause they think it is because of a timeout. If so, how can I extend the timeout time? If not, what are the possible fix for this?

try
{
    string url = Constants.requestUrl + "Host=" + host + "&Database=" + database + "&Request=Fsq6Tr";
    string contentType = "application/json";
    JObject json = new JObject
    {
        { "CAF", caf },
        { "CustomerID", retailerCode },
        { "EmployeeNumber", employeeNumber },
        { "Street", street },
        { "Barangay", barangay },
        { "Town", town },
        { "District", district },
        { "Province", province },
        { "Country", country },
        { "Landmark", landmark },
        { "Telephone1", telephone1 },
        { "Telephone2", telephone2 },
        { "Mobile", mobile },
        { "Email", email },
        { "Location", location },
        { "Date", Convert.ToDateTime(date) },
        { "StartTime", Convert.ToDateTime(startTime) },
        { "EndTime", Convert.ToDateTime(endTime) },
        { "Photo1", photo1 },
        { "Photo2", photo2 },
        { "Photo3", photo3 },
        { "Video", video },
        { "MobilePhoto1", photo1url },
        { "MobilePhoto2", photo2url },
        { "MobilePhoto3", photo3url },
        { "MobileVideo", videourl },
        { "Rekorida", rekorida },
        { "Merchandizing", merchandizing },
        { "TradeCheck", tradecheck },
        { "Others", others },
        { "OtherConcern", otherconcern },
        { "Remarks", remarks },
        { "LastSync", Convert.ToDateTime(current_datetime) },
        { "MobileUpdate", Convert.ToDateTime(current_datetime) }
   };

   HttpClient client = new HttpClient();
   var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));

    await DisplayAlert("Success!", "Form sent successfully!", "Got it");
   }
   catch (Exception ex)
   {
         await DisplayAlert(" Sending Error ", ex.Message, "Got it");
   }

Upvotes: 1

Views: 4698

Answers (2)

Gorosoft
Gorosoft

Reputation: 21

My timeout reason was because device had energy saving mode and it blocks background connections. Maybe it could help someone...

Upvotes: 2

jgoldberger - MSFT
jgoldberger - MSFT

Reputation: 6098

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://learn.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.

Upvotes: 3

Related Questions