Search code examples
c#androidvisual-studioazuresamsung-mobile

Call to Azure web api from android application just hangs forever while awaiting for response


I've published to Azure a very basic asp.net web api code, the one that comes with the template. When I try the default GET action I get JSON response ["value1","value2"]

When I try to make the same call from my xamarin-android project, the execution just hangs forever while awaiting the response (see the code below).

I'm using visual studio 2015. I connected my phone for debugging.

       button.Click += onButtonClick; 
    }

    private void onButtonClick(object sender, EventArgs e)
    {
        GetValuesSync().Wait();
    }

    private async Task GetValuesSync()
    {   
        string ResponseJsonString = null;

        string url =
            "http://myWebapp.azurewebsites.net/api/values";

        using (var httpClient = new HttpClient())
        {
            try
            {
                Task<HttpResponseMessage> getResponse = httpClient.GetAsync(url);
                HttpResponseMessage response = await getResponse; //Execution hangs here forever ...
                ResponseJsonString = await response.Content.ReadAsStringAsync();
                values = JsonConvert.DeserializeObject<string[]>(ResponseJsonString);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }

Thanks for helping


Solution

  • In addition to the answer above, as a best practice, try always specify on which thread the continuation after await should be invoked. In your case , explicitly calling ConfigureAwait(false) should also solve the deadlock problem.