2

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

Swagata Prateek
  • 1,076
  • 7
  • 15
Richard77
  • 20,343
  • 46
  • 150
  • 252
  • 1
    You have deadlock here `GetValuesSync().Wait();`. – Hamlet Hakobyan Aug 20 '16 at 12:59
  • @Richard77: I recommend reading my [MSDN article on async best practices](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx) and my blog post [Don't Block on Async Code](http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). – Stephen Cleary Aug 23 '16 at 02:38

2 Answers2

3

It's always better to leave the async workflow async. Forcefully making it sync would essentially block your UI thread and the app would lose its responsiveness.

What if you try something like this:

private async void onButtonClick(object sender, EventArgs e)
{
    await GetValuesAsync();
}

private async Task GetValuesAsync()
{   
    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;
        }
    }
}

I hope values is not attached to UI or you might have to access the UI thread to update it.

Swagata Prateek
  • 1,076
  • 7
  • 15
1

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.

Xiaoguo Ge
  • 2,177
  • 20
  • 26
  • Indeed, I forgot to add that. Thanks! – Swagata Prateek Aug 20 '16 at 14:41
  • It seems like this one issue is more related to my lack of knowledge of asynchronous programming than in the way xamarin android works. I've a lot of catch ups to do. Can't tell the difference between putting await in the front and calling Wait on the object itself. – Richard77 Aug 21 '16 at 22:36