0

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().

gregoryp
  • 920
  • 4
  • 15
  • 35
  • I think you need some more logging. There is more to a response than the Content. Find out the http status code, headers etc. Also are exceptions eventually logged somewhere? I only see you throwing them up the call stack. – Crowcoder Aug 21 '18 at 18:40
  • While you should not use ConfigureAwait(false) specifically to avoid a deadlock, if you are going to do it anyway you need to do it on all async calls including `ReadAsStringAsync`. – Crowcoder Aug 21 '18 at 18:41
  • I have no log for this operations. I'll do the logging to see if I find something useful. – gregoryp Aug 21 '18 at 18:44
  • @Crowcoder After do the logging, I'm getting an Unauthorized Exception. The question is: why? The apk is the same as the installed locally (company). But the apk in the customer doesn't work. I think this is a problem with newer versions of VS2017. I don't know if its a problem with the API or with the APK. – gregoryp Aug 21 '18 at 20:15
  • @Crowcoder Just found the problem: the token. For some reason the token is incorrect. I comment the [Authorize] on my API and it worked. – gregoryp Aug 21 '18 at 20:24
  • I want to remove the token from my app And see if it would work. – Olorunfemi Davis Mar 04 '19 at 08:39

0 Answers0