Wednesday, 23 October 2019

CancellationToken.ThrowOperationCanceledException

I am getting the following error on my Xamarin.Forms project:

  at System.Threading.CancellationToken.ThrowOperationCanceledException () [0x00010] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at System.Threading.CancellationToken.ThrowIfCancellationRequested () [0x00008] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.<ConnectAsync>b__0 () [0x0004f] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:343 
  at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at System.Threading.Tasks.Task.Execute () [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0 
--- End of stack trace from previous location where exception was thrown ---

  at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x000e4] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:393 
  at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00285] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:286 
  at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in <814c177e4f174da89876fafdde15d02e>:0 
  at BUCOLogin.Constants.RestService.GetBranchesAsync (System.String uri) [0x00047] in C:\VST\BFR Login\Login\Constants\RestService.cs:23 }

After some investigation it seems that a token is being cancelled, but I'm not sure where this token is located or even why its being cancelled. I am using an open API request with no APIKey needed :

Constants.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace BUCOLogin.Constants
{
    public static class Constants
    {
        public const string OpenBabbageEndpoint = "http://IP/ILDTEST/XI/Core/GetBranches";
        //public const string OpenBabbageAPIKey = "f6a04a6c3fbc534c295f6a5e8548e0f6";
    }
}

When stepping through code, it stalls/waits for a while at the following code before giving me the exception:

string content = await response.Content.ReadAsStringAsync();

This code is located in RestService.cs :

using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace BUCOLogin.Constants
{
    public class RestService
    {
        HttpClient _client;

        public RestService()
        {
            _client = new HttpClient();
        }

        public async Task<GetBranches> GetBranchesAsync(string uri)
        {
            GetBranches branchesData = null;
            try
            {
                HttpResponseMessage response = await _client.GetAsync(uri);
                if (response.IsSuccessStatusCode)
                {
                    string content = await response.Content.ReadAsStringAsync();
                    branchesData = JsonConvert.DeserializeObject<GetBranches>(content);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("\tERROR {0}", ex.Message);
            }

            return branchesData;
        }
    }
}

GetBranches.cs :

using System;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json;

namespace BUCOLogin.Constants
{
    public class GetBranches
    {

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("number")]
        public string Number { get; set; }

        [JsonProperty("postcode")]
        public string Postcode { get; set; }

        [JsonProperty("manager")]
        public string Manager { get; set; }

        [JsonProperty("phone")]
        public string Phone { get; set; }

        [JsonProperty("address")]
        public string Address { get; set; }

        [JsonProperty("open")]
        public string Open { get; set; }

        [JsonProperty("close")]
        public string Close { get; set; }

        [JsonProperty("opensat")]
        public string Opensat { get; set; }

        [JsonProperty("closesat")]
        public string Closesat { get; set; }

    }
}

The uri Request does bring back a result on Postman so I am not sure why I am getting this exception and how to move forward.

Thanks in advance.

UPDATE

After Updating the RestService.cs with @Nkosi code, I get the following error :

{System.OperationCanceledException: The operation was canceled.
  at System.Threading.CancellationToken.ThrowOperationCanceledException () [0x00010] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at System.Threading.CancellationToken.ThrowIfCancellationRequested () [0x00008] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.<ConnectAsync>b__0 () [0x0004f] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:343 
  at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at System.Threading.Tasks.Task.Execute () [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0 
--- End of stack trace from previous location where exception was thrown ---

  at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x000e4] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:393 
  at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00285] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:286 
  at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in <814c177e4f174da89876fafdde15d02e>:0 
  at BUCOLogin.Constants.RestService.GetBranchesAsync (System.String uri) [0x00046] in C:\Visual Studio Training\BUCO FR Login\FacialRecognitionLogin\Constants\RestService.cs:18 }


from CancellationToken.ThrowOperationCanceledException

No comments:

Post a Comment