c# - 为什么我的调试器似乎卡在 await 语句中?

标签 c# visual-studio-2017 visual-studio-debugging

我是新手,所以请多多包涵。我正在调试并遇到问题:

var response = await client.GetAsync(uri);

我已经编辑了问题,使其符合最小、完整和可验证的示例。

我在这条语句上跨过调试器以便移动到下一条语句但是由于我仍然不知道的原因,调试器似乎丢失并且无法恢复。

每次我点击 await 调用并跳过时,它每次都会发生。调试器断点就消失了。

全部代码如下:

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();
    }
}

WCF 服务不是问题。它发布在公共(public)服务器上,因此始终可用。我从浏览器调用它,它返回预期的字符串。

我正在使用 VS2017,它在 Android 模拟器中进行调试。

这是断点命中语句时的屏幕截图:

enter image description here

最佳答案

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

这听起来像是一个僵局。由于这是一个 UI 应用程序,因此请进一步检查您的调用堆栈是否有任何阻塞调用,例如 Wait()ResultGetAwaiter().GetResult()。这些可以deadlock if called from the UI thread .

正确的解决方案是将它们更改为await;换句话说,使用 async all the way .

关于c# - 为什么我的调试器似乎卡在 await 语句中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54526935/

相关文章:

c# - 如何获取元素在 StackPanel 中的位置?

c# - ThreadPool.QueueUserWorkItem 的意外行为

c++ - 模板化用法不能嵌套在 Visual Studio 中

c# - Visual Studio C# 2010 快速调试运行速度比发布快

visual-studio-debugging - Excel 2016 在关闭时崩溃并附加了调试器

c# Dapper,SplitOn : multiple same parameter issue Multi-mapping one-to-many

c# - 如何为listviewitem设计UI

vb.net - 从 AnyCPU 更改为 x86 会导致 System.BadImageFormatException 错误

c# - 如何在 Visual Studio 诊断工具窗口中切换进程?

winforms - 无法通过 VPN 调试 - Visual Studio 2010