Search code examples
c#visual-studio-2017visual-studio-debugging

Why my debugger appears to be stuck at an await statement?


I'm new to this, so please bear with me. I am debugging and having an issue with the line:

var response = await client.GetAsync(uri);

I've edited the question so it complies with a Minimal, Complete, and Verifiable example.

I step-over the debugger on this statement so to move at the next statement but for reason still unknown to me, debugger seems to lost and does not recover.

Every time I hit the await call and step-over, it just happens every time. The debugger breakpoint just disappears.

Following is all the code:

public class App : Application // superclass new in 1.3
{
    public App ()
    {
        MainPage = new PinPage { Title = "Pins", Icon = "marker.png" };
    }
}

public class PinPage : ContentPage
{
    private async Task FetchDataAsync()
    {
        HttpClient client = new HttpClient();
        string resultUrl = "http://myuser.gtempurl.com/Service1.svc/GetLocations";
        var uri = new Uri(string.Format(resultUrl, string.Empty));
        var response = await client.GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            string content = await response.Content.ReadAsStringAsync();
            var obj = JsonConvert.DeserializeObject(content);
        }

    }
    public PinPage ()
    {
        FetchDataAsync().GetAwaiter().GetResult();
    }
}

The WCF service is not the issue. It is published in a public server, so it is always available. I call it from a browser and it returns the expected string.

I am using VS2017 and it debugs in an Android Emulator.

Here is a screen capture of when breakpoint hits the statement:

enter image description here


Solution

  • it does not move to the next line. No other line of code is highlighted yellow, no timeout, nothing.

    This sounds like a deadlock. Since this is a UI application, check further up your call stack for any blocking calls like Wait(), Result, or GetAwaiter().GetResult(). These can deadlock if called from the UI thread.

    The proper solution is to change them to await; in other words, use async all the way.