Noel
Noel

Reputation: 373

Xamarin Forms HttpClient GetAsync

I have an ASP.NET WebApi hosted in Azure. There is no HTTP Header based or Azure API authentication on it and fiddler sessions prove that the API is fully functional and spits out data as requested and expected.

In my Xamarin forms (PCL, iOS & Android only) PCL project, I have the following code in a service class:

    public async Task<IEnumerable<Link>> GetCategories()
    {
        IEnumerable<Link> categoryLinks = null;
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Using https to satisfy iOS ATS requirements
            var response = await client.GetAsync(new Uri("https://myproject.azurewebsites.net/api/contents"));

            //response.EnsureSuccessStatusCode(); //I was playing around with this to see if it makes any difference
            if (response.IsSuccessStatusCode)
            {
                var content = response.Content.ReadAsStringAsync().Result;
                categoryLinks = JsonConvert.DeserializeObject<IEnumerable<Link>>(content);
            }
        }
        return categoryLinks;
    }

I have debugged the code and noticed that the control does not go past:

var response = await client.GetAsync(new Uri("https://myproject.azurewebsites.net/api/contents"));

and as a result categoryLinks remains null.

Has anyone come across this before?

A few things to note:

Any assistance on this will be greatly appreciated.

Upvotes: 1

Views: 5040

Answers (1)

Mario Galv&#225;n
Mario Galv&#225;n

Reputation: 4032

I have had this exact problem, and it is being caused by a deadlock, How are you calling this method? usually I go:

Device.BeginInvokeInMainThread(async () => 
{
   var categoriesList = await GetCategories();
});

Or if it is inside your View Model, you can use Task.Run();

Avoid using .Result, and timers within your UI even events handlers may cause deadlocks like this one.

Hope this helps.

Upvotes: 1

Related Questions