I am using C# HttpClient to send HTTP call in my Xamarin.iOS apps. But when I send my app to background or even when the call is sent I open control center or notification center my app crashes gives OperationCanceledException. I am using delegates as callback in these calls when ever I get a response from server. Is there any way to stop my app from crashing. Here is the following example of my code.
public async void LoginVerify(APIDelegate<Response<bool>> callback)
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(IP);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("login_code", Login_Code),
});
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(TimeOutTime));
var result = await client.PostAsync("/dev", content, cts.Token);
string resultContent = await result.Content.ReadAsStringAsync();
Response<bool> response = JsonConvert.DeserializeObject<Response<bool>>(resultContent);
callback(response);
}
}
catch (OperationCanceledException ex)
{
callback(new Response<bool> { ErrorMsg = Params.NoInternet_error, Message = Params.NoInternet_error, Data = false, StatusCode = int.Parse(Params.NoInternet_code) });
}
catch (JsonSerializationException ex)
{
callback(new Response<bool> { ErrorMsg = Params.Main_error, Message = Params.JsonDeserialize_error + " in LoginVerify Function", Data = false, StatusCode = int.Parse(Params.JsonDeserialize_error_code) });
}
catch (Exception ex)
{
callback(new Response<bool> { ErrorMsg = Params.Main_error, Message = ex.Message + " :: " + ex.StackTrace +" in LoginVerify Function", Data = false, StatusCode = int.Parse(Params.AppInternalIssue_code) });
}
}
After I receive the response, I use my delegate like this:
void Login(string ntlogin)
{
try
{
if (!CheckInternetConnection())
{
NoInternetError();
}
else
{
if (toast != null)
toast.Dismiss();
API.Login_Code = code_first.Text + code_second.Text + code_third.Text + code_fourth.Text;
API.LoginVerify((responseVerify) =>
{
if (responseVerify != null)
{
if (responseVerify.StatusCode == 200)
{
if (responseVerify.Data)
{
API.Data((response) =>
{
_timer.Stop();
//If No Exception Is Thrown
if (response != null)
{
if (response.StatusCode == 200 || response.StatusCode == 202)
{
tab. = response.Data;
//del.Window.RootViewController = nav;
UIView.Transition(del.Window, 0.5, UIViewAnimationOptions.TransitionFlipFromRight, () =>
{
del.Window.RootViewController = tab;
}, null);
}
else
{
ErrorLog(response.Message, response.Message + " Error while fetching details on SplashController.", Params.AppInternalIssue_code);
}
}
else
{
ErrorLog(Params.Main_error, " Details returned null on Splash Controller.", Params.AppInternalIssue_code);
}
});
}
}
}
else
{
loadPop.Hide();
ErrorLog(Params.Main_error, responseVerify.Message, Params.AppInternalIssue_code);
}
});
}
}
catch (Exception ex)
{
ErrorLog(Params.Main_error, ex.Message + " :: " + ex.StackTrace + " :: LoginSMSController func Login", Params.AppInternalIssue_code);
}
}
This is the exception that is being raised.
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x001d4] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.16.0.13/src/Xamarin.iOS/Foundation/NSUrlSessionHandler.cs:462
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 /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:506
at API.Entities.Support.SupportAPI.SupportData (API.Entities.Support.SupportAPI+SupportAPIDelegate`1[T] callback) [0x001ab] in /Users/zshakil/Desktop/Projects/Support/API/Entities/SupportAPI.cs:271 }
