I have an Xamarin.Android APK.
When installed in a tablet locally at the company, it works perfectly. I already installed it in about 3 tablets and it is working as expected.
The problem is when I send the APK to be installed in a tablet at my customer (it is located in another state), it seems that the HttpClient is not working as expected.
To test it, I'm showing a Toast
with the response of the request. It should return the Role
of the user.
Locally, it returns the Role
as expected. In the customer, it returns an empty string. No error is thrown locally and in the server as well.
Searching on the web, I found that this could be related with a deadlock. So, I put a .ConfigureAwait(false)
on my Get
method:
public static async Task<string> CheckLoginAsync(string cpf, string password)
{
try
{
_client = new HttpClient();
_client.BaseAddress = new Uri(ApiUrls.Base);
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Vault.CreateToken(cpf));
_client.Timeout = TimeSpan.FromSeconds(Constants.CheckLoginAsyncTimeout);
var url = $"{ApiUrls.UserLoginApp}/{cpf}/{password}";
var result = await _client.GetAsync(url).ConfigureAwait(false);
if (result.IsSuccessStatusCode)
{
var response = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<string>(response);
}
return null;
}
catch (TaskCanceledException tcex)
{
throw new TaskCanceledException("TaskCanceled", tcex.InnerException);
}
catch (Exception ex)
{
throw new LoginUnsuccessfulException("LoginFailedDB", ex.InnerException);
}
}
After that, it started working and the error started in another method. I try to do the same thing using the .ConfigureAwait(false)
but the response is coming null
:
private async Task<AppBaseData> GetAppBaseDataAsync(string username)
{
try
{
_client = new HttpClient();
_client.BaseAddress = new Uri(ApiUrls.Base);
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Vault.CreateToken(username));
_client.Timeout = TimeSpan.FromSeconds(Constants.LoadBaseDataAsyncTimeout);
var url = $"{ApiUrls.SupportAppBaseData}/{username}";
var result = await _client.GetAsync(url).ConfigureAwait(false);
if (result.IsSuccessStatusCode)
{
var response = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<AppBaseData>(response);
}
return null;
}
catch (TaskCanceledException ex)
{
throw new TaskCanceledException("TaskCanceled", ex.InnerException);
}
catch (Exception ex)
{
throw new DataNotReadedException("BaseDataDB", ex.InnerException);
}
}
I have no idea of what is causing this problem since it works with the same APK locally and in the customer is not working. The customer network have already been changed to a wifi tethering and the behavior still continues.
The above methods are inside a Task.Run()
.