简体   繁体   English

DebuggerStepThrough,DebuggerHidden在async-await方法中不起作用

[英]DebuggerStepThrough, DebuggerHidden don't work in an async-await method

When you turn on the "Break when an exception is thrown" feature in the Visual Studio debugger it breaks everywhere for selected exception types. 当您在Visual Studio调试器中打开“抛出异常时断开”功能时,它会在选定的异常类型的任何位置中断。 The way to tell it not to break in a specific method is to decorate these methods with DebuggerStepThrough attribute (or DebuggerHidden ) . 告诉它不要破解特定方法的方法是使用DebuggerStepThrough属性(或DebuggerHidden装饰这些方法

This, evidently , doesn't work for an async method for some reason. 显然 ,出于某种原因,这对于async方法不起作用。 Here's a snippet that reproduces the issue. 这是一个重现问题的片段。 The debugger will break inside the TestAsync even though it's marked with the attributes and it will not break inside Test as excepted ( the only difference between them is the first is marked with the async keyword): 调试器TestAsync内部TestAsync即使它标记了属性,并且它不会Test作为例外(它们之间的唯一区别是第一个用async关键字标记):

public class Attributes
{
    public async Task Run()
    {
        await TestAsync();
        await Test();
    }

    [DebuggerHidden]
    [DebuggerStepThrough]
    public async Task TestAsync()
    {
        try
        {
            throw new Exception("Async");
        }
        catch
        {
        }
        await Task.Delay(100);
    }

    [DebuggerHidden]
    [DebuggerStepThrough]
    public Task Test()
    {
        try
        {
            throw new Exception("sync");
        }
        catch
        {
        }
        return Task.Delay(100);
    }
}

So, is this behavior intended? 那么,这种行为是有意的吗? Is it a bug? 这是一个错误吗? Is there a workaround? 有解决方法吗?

Attributes don't play well with async/await since async methods get re-written under the covers--and the attributes do not follow. 属性与async / await不能很好地兼容,因为异步方法在封面下重写 - 并且属性不会跟随。 See https://stackoverflow.com/a/22412597/495262 for a similar situation. 有关类似情况,请参阅https://stackoverflow.com/a/22412597/495262

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM