Using REFIT throws an error when first time used, how to capture the exception
First time to use Xamarin and I am trying to use a RESTful API for logging in, this code works but the first time I am clicking the BtnConnect it is throwing out an error and I am not sure how can I capture the error, I am using an android device connected thru USB, how can I capture or log the error?
Code:
private async void BtnConnect_Click(object sender, EventArgs e)
{
try
{
txtUsername = FindViewById<TextView>(Resource.Id.txtUsername);
txtPassword = FindViewById<TextView>(Resource.Id.txtPassword);
string username = txtUsername.Text;
string password = txtPassword.Text;
var user = new UserModel
{
username = username,
password = password
};
ApiResponse response = await ApiUtils.GetApiService().Login(user);
Toast.MakeText(this, response.message, ToastLength.Long).Show();
}
catch (Exception ex)
{
Toast.MakeText(this, ex.StackTrace, ToastLength.Long).Show();
}
}
Re: Using REFIT throws an error when first time used, how to capture the exception
Quote:
Originally Posted by
dee-u
First time to use Xamarin and I am trying to use a RESTful API for logging in, this code works but the first time I am clicking the BtnConnect it is throwing out an error and I am not sure how can I capture the error, I am using an android device connected thru USB, how can I capture or log the error?
Code:
private async void BtnConnect_Click(object sender, EventArgs e)
{
try
{
txtUsername = FindViewById<TextView>(Resource.Id.txtUsername);
txtPassword = FindViewById<TextView>(Resource.Id.txtPassword);
string username = txtUsername.Text;
string password = txtPassword.Text;
var user = new UserModel
{
username = username,
password = password
};
ApiResponse response = await ApiUtils.GetApiService().Login(user);
Toast.MakeText(this, response.message, ToastLength.Long).Show();
}
catch (Exception ex)
{
Toast.MakeText(this, ex.StackTrace, ToastLength.Long).Show();
}
}
So what error is it throwing?
Re: Using REFIT throws an error when first time used, how to capture the exception
Was overthinking this, I realized that I could log the exception in the Catch:
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
Console.WriteLine(ex.StackTrace);
Toast.MakeText(this, ex.StackTrace, ToastLength.Long).Show();
This was the logged messages:
Quote:
02-29 21:45:44.904 I/mono-stdout( 9934): An error occurred while sending the request.
An error occurred while sending the request.
The server returned an invalid or unrecognized response.
02-29 21:45:44.933 I/mono-stdout( 9934): The server returned an invalid or unrecognized response.
at System.Net.Http.HttpConnection.SendAsyncCore (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x012d9] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs:718
02-29 21:45:45.111 I/mono-stdout( 9934): at System.Net.Http.HttpConnection.SendAsyncCore (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x012d9] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs:718
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync (System.Net.Http.HttpConnection connection, System.Net.Http.HttpRequestMessage request, System.Boolean doRequestAuth, System.Threading.CancellationToken cancellationToken) [0x000e6] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:316
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync (System.Net.Http.HttpRequestMessage request, System.Boolean doRequestAuth, System.Threading.CancellationToken cancellationToken) [0x00101] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:296
02-29 21:45:45.112 I/mono-stdout( 9934): at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync (System.Net.Http.HttpConnection connection, System.Net.Http.HttpRequestMessage request, System.Boolean doRequestAuth, System.Threading.CancellationToken cancellationToken) [0x000e6] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:316
at System.Net.Http.RedirectHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00070] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs:32
02-29 21:45:45.112 I/mono-stdout( 9934): at System.Net.Http.HttpConnectionPool.SendWithRetryAsync (System.Net.Http.HttpRequestMessage request, System.Boolean doRequestAuth, System.Threading.CancellationToken cancellationToken) [0x00101] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:296
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x000b3] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:531
02-29 21:45:45.113 I/mono-stdout( 9934): at System.Net.Http.RedirectHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00070] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs:32
at Refit.RequestBuilderImplementation+<>c__DisplayClass14_0`2[T,TBody].<BuildCancellableTaskFuncForMethod>b__0 (System.Net.Http.HttpClient client, System.Threading.CancellationToken ct, System.Object[] paramList) [0x00188] in <cda9777f03ee4e9188064495e9f2e568>:0
02-29 21:45:45.114 I/mono-stdout( 9934): at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x000b3] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:531
at App4.MainActivity.BtnConnect_Click (System.Object sender, System.EventArgs e) [0x000b8] in C:\Users\xxx\source\repos\App4\App4\MainActivity.cs:62
02-29 21:45:45.115 I/mono-stdout( 9934): at Refit.RequestBuilderImplementation+<>c__DisplayClass14_0`2[T,TBody].<BuildCancellableTaskFuncForMethod>b__0 (System.Net.Http.HttpClient client, System.Threading.CancellationToken ct, System.Object[] paramList) [0x00188] in <cda9777f03ee4e9188064495e9f2e568>:0
02-29 21:45:45.116 I/mono-stdout( 9934): at App4.MainActivity.BtnConnect_Click (System.Object sender, System.EventArgs e) [0x000b8] in C:\Users\xxx\source\repos\App4\App4\MainActivity.cs:62
d response.
It only happens the first time I click the button, the succeeding clicks doesn't raise the above exception.
Re: Using REFIT throws an error when first time used, how to capture the exception
I am using the Retry pattern as a "temporary" fix: https://docs.microsoft.com/en-us/azu...patterns/retry
It has been recommended in the following case study: https://4lowtherabbit.github.io/blogs/2018/11/CaseStudy
If anyone has other recommended solution then please hit the reply button, I am leaving this open for now.